11 Replies - 63431 Views - Last Post: 30 June 2010 - 06:36 PM Rate Topic: -----

#1 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

ArrayList Of Objects

Posted 27 November 2008 - 03:19 PM

I'm working on this program:

Quote

The list of students has the following data structure:
Student's Name
History grade
Math Grade
English grade

Your program will give the user the following choices:
a. The computer will display the name of all the students as well as the overall average of the three subject marks that are in the data list.
b. The computer will display those students that earned greater than 80% in history class.
c. The computer will display only those students that passed ALL of their courses.
d. The computer will display the those students that passed either Math or English.
e. The computer will display those students that failed no courses or just one.


Using a class and I am going about creating an array list of objects. The program reads in the name and three marks into a double array and then passes the name and the array to an object, and adds the object to the array list. Just before I add the object to the array it has the correct value but for some unknown reason after I have added the object it doesn't contain the array of marks I want. It contains the array of whatever marks are in the end of the file.

My driver:

package ss9;

import java.util.*;
import java.io.*;

/**
 *
 * @author brocj1112
 */
public class e91f {

	public static void main(String[] args) throws IOException {
		Scanner fin = new Scanner(new FileReader("IN/e91fIN.txt"));
		Scanner sin = new Scanner(System.in);
		String sName;
		double[] ardMarks = new double[3];
		int nChoice;
		students s;
		ArrayList<students> alsStudents = new ArrayList<students>();

		while (fin.hasNextLine()) {
			sName = fin.next() + " " + fin.next();
			for (int i = 0; i < 3; i++) {
				ardMarks[i] = fin.nextDouble();
			}
			s = new students(ardMarks, sName);
			//s.all();
			alsStudents.add(s);
		}
		
		
		
		
		
		
		displayMenu();
		nChoice = sin.nextInt();
		out(alsStudents,nChoice);
		fin.close();
	}

	public static void displayMenu() {

		System.out.println("Do you want to: ");
		System.out.println("\t1. Display averages of all students");
		System.out.println("\t2. Display all those with 80% in history");
		System.out.println("\t3. Display those that passed all classes");
		System.out.println("\t4. Display those that passed either Math or English");
		System.out.println("\t5. Displayed those that failed no courses or one.");
	}

	static void out(ArrayList<students> alsStudents, int nChoice) {
		if (nChoice == 1) {
			for (int i = 0; i < alsStudents.size(); i++) {
				alsStudents.get(i).calcAverage();
			}
		} else if (nChoice == 2) {
			for (int i = 0; i < alsStudents.size(); i++) {
				alsStudents.get(i).history();
			}
		} else if (nChoice == 3) {
			for (int i = 0; i < alsStudents.size(); i++) {
				alsStudents.get(i).all();
			}
		} else if (nChoice == 4) {
			for (int i = 0; i < alsStudents.size(); i++) {
				alsStudents.get(i).mathOrEnglish();
			}
		} else if (nChoice == 5) {
			for (int i = 0; i < alsStudents.size(); i++) {
				alsStudents.get(i).failedOne();
			}
		}
	}
}



and my class:

package ss9;

/**
 *
 * @author brocj1112
 */
class students {

	
	String sName;
	/**
	 * ardMark[0] is history
	 * ardMark[1] is Math
	 * ardMark[2] is English
	 */
	double[] ardMarks = new double[3];
	double dAverage;

	
	
	students(double[] _ardMarks, String _sName) {
		sName = _sName;
		ardMarks = _ardMarks;
	}

	void history() {
		/**
		 * this method displays all who got more than 80% in history
		 */
		if (ardMarks[0] > 80) {
			System.out.println(sName);
		}
	}

	void calcAverage() {
		double dSum = 0;
		for (int i = 0; i < ardMarks.length; i++) {
			dSum += ardMarks[i];
		}
		dAverage = dSum / ardMarks.length;

		System.out.format(sName + " %.2f", dAverage);
		System.out.print("\n");
	}

	void all() {
		/**
		 * this method displays those that passed all classes
		 */
		if (ardMarks[0] > 50 && ardMarks[1] > 50 && ardMarks[2] > 50) {
			System.out.println(sName);
		}
	}

	void mathOrEnglish() {
		if (ardMarks[1] > 50 || ardMarks[2] > 50) {
			System.out.println(sName);
		}
	}

	void failedOne() {
		int nFailed = 0;
		for (int i = 0; i < 3; i++) {
			if (ardMarks[i] < 50) {
				nFailed++;
			}
		}

		if (nFailed == 1 || nFailed == 0) {
			System.out.println(sName);
		}
	}
}



because for some strange reason the objects aren't being added to the array with the correct array of marks.

Any ideas as to why?

My input file:

Quote

Ahmed Chen 81 80 70
Brian Morouney 80 83 72
Tommy Tong 32 65 52
Chad Wong 80 74 92
Carole Faist 56 23 10
Ethyl Gas 50 60 20
Ali Sally 24 56 90


When I ask the program to show the average of all students I get:

Quote

Ahmed Chen 56.67
Brian Morouney 56.67
Tommy Tong 56.67
Chad Wong 56.67
Carole Faist 56.67
Ethyl Gas 56.67
Ali Sally 56.67


but the output I should be getting is:

Quote

Ahmed Chen 70.33
Brian Morouney 78.33
Tommy Tong 49.67
Chad Wong 82.00
Carole Faist 29.67
Ethyl Gas 43.33
Ali Sally 56.67


Any ideas as to how this is happening?

Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList Of Objects

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ArrayList Of Objects

Posted 27 November 2008 - 07:38 PM

I hate scanners you never know how they handle end of line
I always read line by line even if I need another scanner to parse the read lines

   Scanner readFile = new Scanner(new File("filename.txt"))
   while(readFile.hasNext())
   {
	   Scanner line = new Scanner(readFile.nextLine());
	   String name = line.next();
	   name = name + " " + line.next();
	   double[] mark = new double[3];
	   mark[0] = line.nextDouble();
	   ....
	   Student s = new Student(.....
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: ArrayList Of Objects

Posted 29 November 2008 - 05:13 AM

I tried that and it's still doing the same thing as above. This is really strange. :(

It's like everytime through it updates the array in every object with the last array but I cannot see why it is doing that. :(
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: ArrayList Of Objects

Posted 29 November 2008 - 01:58 PM

Your problem is here:

 students(double[] _ardMarks, String _sName) {
        sName = _sName;
        ardMarks = _ardMarks;	   
    }

//it should be:

 students(double[] _ardMarks, String _sName) {
        sName = _sName;
        for(int i = 0; i < 3; i++)
        {
        	ardMarks[i] = _ardMarks[i];	
        }
        
    }



Not entirely sure about java, but in C++ you can't just assign one array to another in one blanket statement, you have to assign each index. I ran it and it works as intended. :)

Do you want to: 
    1. Display averages of all students
    2. Display all those with 80% in history
    3. Display those that passed all classes
    4. Display those that passed either Math or English
    5. Displayed those that failed no courses or one.
1
Ahmed Chen 77.00
Brian Morouney 78.33
Tommy Tong 49.67
Chad Wong 82.00
Carole Faist 29.67
Ethyl Gas 43.33
Ali Sally 56.67

Process completed.



edited for typos

This post has been edited by KYA: 29 November 2008 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#5 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: ArrayList Of Objects

Posted 29 November 2008 - 02:04 PM

Thanks a lot, now it makes sense. I didn't know about that, I thought that worked but I guess not. Thanks man. :D
Was This Post Helpful? 0
  • +
  • -

#6 Guest_x_ray*


Reputation:

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:22 PM

View Postchili5, on 29 November 2008 - 01:04 PM, said:

Thanks a lot, now it makes sense. I didn't know about that, I thought that worked but I guess not. Thanks man. :D


It's quite late to respond but I'm not sure if that worked for you. I had similar problem and here is what i did;
instead of
alsStudents.add(s) change to
alsStudents.add( new Student(blah blah));
Was This Post Helpful? 0

#7 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:25 PM

hahaha necro'd! :P
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:28 PM

@x_ray: I appreciate your willingness to help. However, as this thread is a couple years old, I don't think the OP will be checking back anytime soon. For these reasons, please avoid necroposting in the future. :)
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:29 PM

Mac updated me. Thanks buddy! :)

This post has been edited by Dogstopper: 30 June 2010 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:32 PM

@Dogstopper: Per the team blog, locked threads are not indexed in Google searches. And as this thread has useful and relevant information that others could find helpful, I am going to re-open this thread. :)
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:35 PM

I totally missed that. I have been gone way too long. Thanks for the heads up.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: ArrayList Of Objects

Posted 30 June 2010 - 06:36 PM

Not a problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1