Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 26, 2006, 09:17 PM   #1
prostuff1
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Need some help with my java lab

I will start by giving a link to the instructions for this project:
link

To be honest i dont know were to start (well i kinda do but i can't get anything to work the way i think it should).

I have some code written down but it is the really obvious stuff.

If anyone can look at the direction for the lab and point me in the right direction that would be a big help and greatly appreciated.

Thanks

here is the code that i have and like i said before it is the obvious stuff:
Code:
import java.util.Scanner;
import java.io.*;
public class Lab7
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		String filename;
		System.out.print("Enter input file name: ");
		filename = keyboard.nextLine();
		FileReader freader = new FileReader(filename);
		BufferedReader inputfile = new BufferedReader(freader);
		int numberOfStudents = FileIOHelper.getNumberOfStudents(filename);
		System.out.println(numberOfStudents);
	}
}
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old May 26, 2006, 11:44 PM   #2
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by prostuff1
To be honest i dont know were to start (well i kinda do but i can't get anything to work the way i think it should).

I have some code written down but it is the really obvious stuff.
Here's where you start: break the problem down piece by piece and solve the subproblems individually.

In this case, you need to read a file and output some stuff about the file. So look at the expected output and figure out how you generate each line. You got the first one already, now how about the next two? They're pretty obvious too. Just print out those column headings verbatim and then a line of hypens.

Now think about the students problem. Each line has to print out data about one student, as well as sign indicating that student's relationship to the average. Therefore, you're going to have to load all of the students before you begin printing them out. The assignment asks you to create a Student class, so create that. What fields does it need to have? Once you've got that class, you need to loop through the input file records and create Student instances for each record -- looping through objects generally implies that you'll want to store them in an array (e.g. Student[]). Once you've got all your objects created, then you need to loop through them again to calculate the total scores and average score and the min/max statistics.

Then print out all the information you've put together...pretty easy really. Do you feel comfortable following the steps above?
__________________
Mehce
savar is offline   0 Reply With Quote
Old May 27, 2006, 02:52 PM   #3
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Quote:
Originally Posted by savar
Here's where you start: break the problem down piece by piece and solve the subproblems individually.

In this case, you need to read a file and output some stuff about the file. So look at the expected output and figure out how you generate each line. You got the first one already, now how about the next two? They're pretty obvious too. Just print out those column headings verbatim and then a line of hypens.

Now think about the students problem. Each line has to print out data about one student, as well as sign indicating that student's relationship to the average. Therefore, you're going to have to load all of the students before you begin printing them out. The assignment asks you to create a Student class, so create that. What fields does it need to have? Once you've got that class, you need to loop through the input file records and create Student instances for each record -- looping through objects generally implies that you'll want to store them in an array (e.g. Student[]). Once you've got all your objects created, then you need to loop through them again to calculate the total scores and average score and the min/max statistics.

Then print out all the information you've put together...pretty easy really. Do you feel comfortable following the steps above?
Thanks for the help. I was not thinking straight last night. I should have just went to bed and worked on it in the morning.

Anyway, I do understand some of this. BUt this is first time we have had to do this kind of stuff in my java class and the teacher did not explain it very well.

Anyway, i have some (and i am not quite sure if it is the correct way) of it working. I can get all the student names in...but not there scores

Here is what i have so far:
Code:
import java.util.Scanner;
import java.io.*;
public class Lab7
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		String filename;
		int studentNum = 0;
		System.out.print("Enter input file name: ");
		filename = keyboard.nextLine();
		System.out.println();
		FileReader freader = new FileReader(filename);
		BufferedReader inputfile = new BufferedReader(freader);
		int numberOfStudents = FileIOHelper.getNumberOfStudents(filename);
		System.out.println("Name\t\t\tScore1\tScore2\tScore3\tTotal");
		System.out.println("----------------------------------------------------------");
		Student nextStudent = FileIOHelper.getNextStudent();
		String student = nextStudent.getName();
		while(studentNum <= numberOfStudents)
		{
			System.out.println(student);
			nextStudent = FileIOHelper.getNextStudent();
			student = nextStudent.getName();
			studentNum++;
		}
	}
}
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old May 27, 2006, 04:03 PM   #4
rand()
macrumors regular
 
Join Date: Jul 2004
Location: Michigan
You're definitely on the right track here.

Have you covered arrays yet? You can do *almost* everything required here without them, except the "+, -, =" thing. Arrays are definitely going to make that part easier.

Essentially, you'll need to store the Student objects so that you can revisit them. You need to calculate their totals, as well as calculating the average of the class before you print anything out.

Work on it a bit longer, and come back if you get stuck again.

EDIT: Yep, you definitely need arrays. Your syllabus says that's the topic of this lab!
__________________
MacBook x2, iPod touch 1g, and Logitech accessories

Last edited by rand(); May 27, 2006 at 04:08 PM.
rand() is offline   0 Reply With Quote
Old May 28, 2006, 04:10 AM   #5
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
OK, i set down this afternoon and got most everything done. The only thing i have left is the "+,-,=" thing.

Hear is the code i have so far:
Code:
import java.util.Scanner;
import java.io.*;
public class messwith
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		int totalScore = 0;
		int max = 0;
		int min = 0;
		int sumScore = 0;
		String maxName = "";
		String minName = "";
		int avgScore = 0;
		System.out.print("Enter input file name: ");
		String filename = keyboard.nextLine();
		System.out.println();
		FileReader freader = new FileReader(filename);
		BufferedReader inputfile = new BufferedReader(freader);
		int numberOfStudents = FileIOHelper.getNumberOfStudents(filename);
		System.out.println("Name\t\t\tScore1\tScore2\tScore3\tTotal");
		System.out.println("----------------------------------------------------------");
		for(int index = 0; index < numberOfStudents; index++)
		{
			Student std = FileIOHelper.getNextStudent();
			System.out.print(std.getName() + "\t\t");
			for(int num = 1; num <= 3; num++)
			{
				int score1 = std.getScore(num);
				System.out.print(score1 + "\t"); 
				totalScore = totalScore + std.getScore(num);
			}
			sumScore = sumScore + totalScore;
			avgScore = sumScore/numberOfStudents;
			System.out.print(totalScore);
			if(max < totalScore)
			{
				max = totalScore;
				maxName = std.getName();
			}
			min = max;
			if(totalScore < min )
			{
				min = totalScore;
				minName = std.getName();
			}
			
			totalScore = 0;
			System.out.print("\n");
		}
		System.out.println("----------------------------------------------------------");
		System.out.println("The total number of students in this class is: \t" + numberOfStudents);
		System.out.println("The average total score of the class is: \t\t" + avgScore);
		System.out.println(maxName + " got the maximum total score of: \t" + max);
		System.out.println(minName + " got the minimum total score of: \t" + min);
		System.out.println("----------------------------------------------------------");
	}
}
The only thing i have left to do is the "+,-,=" thing.

Thanks for the help guys/gals.
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Jun 1, 2006, 04:14 PM   #6
MarkCollette
macrumors 65816
 
MarkCollette's Avatar
 
Join Date: Mar 2003
Location: Calgary, Canada
Take out a peice of paper, or use some text editor program, and write out what the program is supposed to do:

1. Read in input file
2. Do calculations
3. Output stats

Now, break down each step with stepwise refinement. Eg:

1. Read in input file

becomes

1.1 Get filename
1.2 Open file
1.3 Read number of students
1.4 Read each student object


Just keep breaking down the details until each line is pseudocode, or real Java code.

Now, obviously this advice is a little late, since you're half done your program already, but keep it in mind for next time. It might help you avoid bugs, like:

for(int index = 0; index < numberOfStudents; index++) {
...
avgScore = sumScore/numberOfStudents;
...
}
MarkCollette is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 04:11 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC