6 Replies - 2478 Views - Last Post: 05 February 2013 - 10:38 AM Rate Topic: -----

#1 ladyJava  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 24-January 13

My First Java Project. Need some insight

Posted 03 February 2013 - 09:03 PM

So i'm starting my project and I know the project seems really simple to many advance coders.My obstacle is the vagueness of the instructions. I'm puzzled on where i'm suppose to start.

The Instructions: Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods getName(), addQuiz(int score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took.

Supply a StudentTester class that tests all methods.


What I have so far :


package edu.westga.cs1301.student;

public class Student {

	private String student;
	private int score;
	
	public Student()
	{
		
		student = new String("John Doe");
		score= 0;
	}
	
	/**
	 * This method will get the name of the student
	 */
	
	public void getName()
	{
		
	}
	
	/**
	 * This method allows the user to input the quiz score
	 */
	
	public void addQuiz(int score)
	
	{
		
	}
	
	/**
	 * This method gets the total score of the quiz
	 */

	public void getTotalScore()
	
	{
		
	}
	
	/**
	 * This method gets the average score of all the quizzes
	 */
	
	public void getAverageScore()
	{
		
	}
	
	/**
	 * 
	 */

}


Am I suppose to just name my own students?
If so, why is there getName()method, better yet, how would I use it?
Also, am I suppose to just input the quiz scores myself?

Best Regards,

ladyJava

Is This A Good Question/Topic? 0
  • +

Replies To: My First Java Project. Need some insight

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: My First Java Project. Need some insight

Posted 03 February 2013 - 09:20 PM

Quote

Am I suppose to just name my own students?

I would say no
You would like to create new instances of Student, one for each student participating in the quiz :)
Instead of making redundant code, you will want to reuse the Student class for all of the students
For example if 5 students are taking the quiz, you will need 5 student instances
If I were to use a Student class, I would like to supply the name of the student as an argument
Student peter = new Student("Peter");
Student susan = new Student("Susan");


Was This Post Helpful? 2
  • +
  • -

#3 ladyJava  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 24-January 13

Re: My First Java Project. Need some insight

Posted 03 February 2013 - 09:23 PM

View PostCasiOo, on 03 February 2013 - 09:20 PM, said:

Quote

Am I suppose to just name my own students?

I would say no
You would like to create new instances of Student, one for each student participating in the quiz :)/>/>
Instead of making redundant code, you will want to reuse the Student class for all of the students
For example if 5 students are taking the quiz, you will need 5 student instances
If I were to use a Student class, I would like to supply the name of the student as an argument
Student peter = new Student("Peter");
Student susan = new Student("Susan");



That make sense ! Wow, I cant wait till I will be able to disect instructions and just come up with a solution.
Thank you so much for you input, Im on it now! (:
Was This Post Helpful? 0
  • +
  • -

#4 ladyJava  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 24-January 13

Re: My First Java Project. Need some insight

Posted 04 February 2013 - 11:19 AM

Okay, so by looking at the instructions over and over and over.

This is what ive came up with so far :

package edu.westga.cs1301.student;

public class Student {

	private String Student;

	
	private int totalQuizScore;
	
	public Student( String Student, int totalQuizScore)
	{
		totalQuizScore= 0;
		Student peter = new Student("Peter", totalQuizScore);
		Student john=new Student("John", totalQuizScore);
		
	}
	
	/**
	 * This method will get the name of the student
	 */
	
	public String getName()
	{
		return this.Student;
		
	}
	
	/**
	 * This method allows the user to input the quiz score
	 */
	
	public void addQuiz(int score)
	
	{
		
	}
	
	/**
	 * This method gets the total score of the quiz
	 */

	public void getTotalScore()
	
	{
		
	}
	
	/**
	 * This method gets the average score of all the quizzes
	 */
	
	public void getAverageScore()
	{
		
	}
	
	/**
	 * 
	 */

}




Im not sure how to use the return.this as well.
Should I just use System.out.println(String)for the method getName()?
Since their is no public void static main(String[] args), I cant really test it out to see...
Was This Post Helpful? 0
  • +
  • -

#5 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: My First Java Project. Need some insight

Posted 05 February 2013 - 01:32 AM

Quote

Supply an appropriate constructor

You can use a default constructor or a constructor with arguments. In this case, I would assume they want you to use a default constructor.

Quote

Supply a StudentTester class that tests all methods

They are talking about composition. It means you will use the functionality of one class inside of another class. In this case, you will use the functionality of Student inside of StudentTester. It is really unnecessary for this kind of exercise but whatever...

Quote

Am I suppose to just name my own students?
If so, why is there getName()method, better yet, how would I use it?
Also, am I suppose to just input the quiz scores myself?

They probably want you to use setters and getters to call the fields.

Quote

Im not sure how to use the return.this as well.

I think in your case, you will probably want to use this.name = name in a setName() method.

Quote

Should I just use System.out.println(String)for the method getName()?

Yes, you can call getName() with an object reference inside of println()

Quote

Since their is no public void static main(String[] args), I cant really test it out to see...

You will create a main() inside of your StudentTester class.

The code would look something like this
class Student {

	private String name;

	private int score;
	
	public Student()
	{
		
	}
	
	public Student(String name, int score)
	{
		this.name = name;
		this.score = score;
	}
	
	public String toString()
	{
		return "My name is " + name + " and my score is " + score; 
	}
	
	
	
	/**
	 * This method will get the name of the student
	 */
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * This method allows the user to input the quiz score
	 */
	
	public void setTotalScore(int score)
	
	{
		this.score = score;
	}
	
	/**
	 * This method gets the total score of the quiz
	 */

	public int getTotalScore()
	
	{
		return score;
	}
	
	/**
	 * This method gets the average score of all the quizzes
	 */
	
	public void getAverageScore()
	{
		
	}
	
	/**
	 * 
	 */
}

public class StudentTester
{
	public static void main(String [] args)
	{
		// Demo #1 - Default Constructor
		Student student1 = new Student();
		
		student1.setName("Peter");
		student1.setTotalScore(95);
		
		System.out.println("My name is " + student1.getName() + " and my score is " + student1.getTotalScore());
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		// Demo #2 - Using Constructor with arguments
		Student s1 = new Student("John", 88);
		System.out.println(s1);
		
	}
}


For the sake of simplicity, I changed some things around. I changed totalQuizScore to score. I also added setters and getters for those fields. I split the code into two parts for demonstration purposes.

In the first part, I used a default constructor and used setters and getters to manipulate the fields. [Note: See that I explicitly declared a do-nothing default constructor. The compiler can write a default constructor for you but, in this case, it had to be declared explicitly because there is another constructor that is non-default].

In the second part, a constructor with arguments is used. In this case, toString() of class Object is overridden so that the object reference s1 could be printed.

You can see that Demo#2 is a little bit cleaner and easier to manipulate the data but your instructors probably have not gotten this far in your class yet, so I would stick with Demo#1 for now.

Hope this helps :)
Was This Post Helpful? 2
  • +
  • -

#6 ladyJava  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 24-January 13

Re: My First Java Project. Need some insight

Posted 05 February 2013 - 10:32 AM

Thanks so much jjho8 ! I turned in my project and it looks great !
Was This Post Helpful? 0
  • +
  • -

#7 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: My First Java Project. Need some insight

Posted 05 February 2013 - 10:38 AM

View PostladyJava, on 05 February 2013 - 10:32 AM, said:

Thanks so much jjho8 ! I turned in my project and it looks great !

You're welcome. Glad to help :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1