Need some help with my java lab

Discussion in 'Mac Programming' started by prostuff1, May 26, 2006.

  1. macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #1
    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);
    	}
    }
    
     
  2. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #2
    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?
     
  3. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #3
    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++;
    		}
    	}
    }
    
     
  4. macrumors regular

    Joined:
    Jul 15, 2004
    Location:
    Michigan
    #4
    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!
     
  5. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #5
    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.
     
  6. macrumors 65816

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #6
    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;
    ...
    }
     

Share This Page