Lab Assignment #9
Overview
- Part A: Add a new field/instance variable to a class definition.
- Surveys: Individually complete a few surveys to give us feedback on this course.
- Quiz: Each
person will individually take a short quiz. The quiz this week is extremely short and won't take long to complete.
- Part B (optional, for more practice): Add the accessor/getter methods for the new field added in Part A.
Part A
Copy
- Right click on the Desktop and Select "Open a Terminal"
- Paste the following lines of code into the terminal
- cd ../public/Lab9
- make directories
- make install
- cd
- cd Lab9
- mv Lab9.java.empty Lab9.java
- mv Student.java.empty Student.java
- ls
- The previous command should print out two filenames: Lab9.java Student.java
Read and Understand the Code:
For Part A we are going to have you add another field/instance variable to the class Student. Student is a class that was introduced to you in your book (Chapter 11, page 343). This class has:
For Part A we are going to have you add another field/instance variable to the class Student. Student is a class that was introduced to you in your book (Chapter 11, page 343). This class has:
- Two fields or instance variables: name – the name of the student and gradeArray – the array of grades for the student.
- Three constructors: one that takes no arguments, one that takes just a name, and one that takes a name and a gradeArray
- Five accessor/mutator methods: two getters – each returns one of the fields and three setters – each sets one of the fields (two set the same field, gradeArray, but in different ways).
- One toString method – returns the String representation of a Student (that is a String one could use to print out the important information about a student).
- Open Student.java in Dr. Java. You should look through the code to make sure you understand each of these fields, constructors, and methods. If you do not understand please ask the teacher or tutor for help
- Move your cursor to find the declarations of each field
- What are the names of the 2 mutator methods that can be used to set the gradeArray field? Can you describe when you would want to use which method?
- NOW it's time for you to add a field to the student class. Let's add a field named major. This field will be of type String and it will be private. What line should you add to Student.java to define this instance variable?
- You should have added a line private String major; underneath private double[] gradeArray; This will let Java know that every time a Student object is "created" (by calling new Student(), for example), it should allocate space to store the student's name (a String), their grades (an array of doubles), and their major (another String). However, just adding a new field doesn't specify how to initialize (or what to store) in the major instance variable. This is done in a constructor (sometimes abbreviated ‘ctor’). Let's look at the ctors already provided.
- If you construct a Student with no initial information (using the default, no-argument constructor), what name is the Student given? How much space for that Student's grades does it create? What grades are initially given to the student?
- If you construct a Student like this:
double[] grades = { 99, 5, 33.3 };
Student foo = new Student( "Jose", grades );
Which constructor is executed? (What constructor method header)
When that constructor is done executing, what is the value of gradeArray.length?
- Since you added a new field to Student, do you need to modify the constructors? When you create an object, all fields should be initialized. If a Student is created using only a name, their major should be initialized to "Undecided". Similarly if you create a Student with no arguments. Make the appropriate changes to these two constructors to accommodate this new instance variable. Ask a tutor to check your work.
- We usually have a constructor that takes as parameters values for ALL fields in a class. Can you modify the third constructor to support this? HINT: You need to MODIFY one line, and ADD one line of code
- Lastly, when we want to print out the information of a Student, it would be useful to know their major. What method should we modify for this?
- We'll need to modify the return value in the toString() method to be
return "Student object named: " + this.name + " Average: " + this.getAverage() + " Major: " + this.major;
Compile and Run
Compile and run the tester application, Lab9.java. Your program should print out something that looks like:
Student object named: Joe Student Average: 0.0 Major: Undecided
Student object named: Shaun White Average: 0.0 Major: Undecided
Student object named: Body Miller Average: 3.333333333335 Major: Computer Science
If it does not match, ask the teacher or tutor for help.
Compile and run the tester application, Lab9.java. Your program should print out something that looks like:
Student object named: Joe Student Average: 0.0 Major: Undecided
Student object named: Shaun White Average: 0.0 Major: Undecided
Student object named: Body Miller Average: 3.333333333335 Major: Computer Science
If it does not match, ask the teacher or tutor for help.
Surveys
You will need to complete your surveys individually, so if you brought your own laptop, please get it out now. If you don't have a laptop, find an empty computer in this lab. If there is no empty computer, a tutor will help you find an empty computer in a nearby lab.
Complete the following three surveys in this order:
1. Your CAPES (you should have received an email with a link)
3. Your TA/tutor evaluation (you should have received an email with a link)
If you have already completed these surveys, obviously you don't have to complete them again. But you must complete these surveys before taking your lab quiz.
Complete the following three surveys in this order:
1. Your CAPES (you should have received an email with a link)
3. Your TA/tutor evaluation (you should have received an email with a link)
If you have already completed these surveys, obviously you don't have to complete them again. But you must complete these surveys before taking your lab quiz.
Log Out
Click on System > Log Out cs8wzz... > Log Off
Complete Your Lab Quiz
See the instructor to get your individual lab quiz.
This week the quiz will be simply to record your attendance, you do not have to answer the questions to receive full credit.
This week the quiz will be simply to record your attendance, you do not have to answer the questions to receive full credit.
Part B (optional): Do this after the lab if you want more practice
When asking for help outside of the lab period you may post to Piazza and/or speak to tutors in the lab
Read and Understand the Code
Read and Understand the Code
- When writing classes it is important to be able to modify fields in an object after the object has been created. For example, when a Student is first created, she/he may not know what her/his major is (their major is Undecided), but after taking a few classes they realize they LOVE computer science and want to change their major. The Student object should not have to be re-created every time a student switches majors; instead we should be able to access the fields of the Student object and modify them.
- Look at the accessor/mutator methods (get and set methods) in Student.java (they start after a comment saying PART B STARTS HERE). If you do not understand these methods, please ask.
- Before you add your own:
- What method header should you use for a "getter" for the major field?
- What method header should you use for a "setter" for the major field?
- Write the get and set methods for the major field you added in Part A. If you are confused, please ask for help.
Compile and Run
Now comment the code in Lab9.java for PartA and uncomment the code in Lab9.java for Part B. Compile and run Lab9.java. Your output should match this:
Name was successfully set: true
The first grade was successfully set: true
The major was successfully set: true
Student object named: Apolo Ohno Average: 0.4 Major: Bioengineering
Apolo Ohno has changed his mind and realized CS is much better
Apolo Ohno has a new major! It is: Computer Science
If it does not, please ask.
Now comment the code in Lab9.java for PartA and uncomment the code in Lab9.java for Part B. Compile and run Lab9.java. Your output should match this:
Name was successfully set: true
The first grade was successfully set: true
The major was successfully set: true
Student object named: Apolo Ohno Average: 0.4 Major: Bioengineering
Apolo Ohno has changed his mind and realized CS is much better
Apolo Ohno has a new major! It is: Computer Science
If it does not, please ask.
Sample Code
import
java.io.*;
import java.util.*;
/**
* Class that describes a student. A student has a name and an
* array of grades. You can get information about a student
* such as her/his name and grade average.
*
* @author Barb Ericson [email protected]
*/
public class Student
{
//////////// fields //////////////////
/** the name of the student: first last */
private String name;
/** an array of grades for this student */
private double[] gradeArray;
//////////// constructors ///////////
/**
* No argument constructor. Leaves
* all fields with default values
*/
public Student() {
this.name = "Joe Student";
this.gradeArray = new double[10];
}
/**
* Constructor that takes the name
* @param theName the student's name
*/
public Student(String theName)
{
this.name = theName;
this.gradeArray = new double[10];
}
/**
* Constructor that takes the name
* and an array of grades
* @param theName the student's name
* @param theGradeArray the array of grades
*/
public Student(String theName,
double[] theGradeArray)
{
this.name = theName;
this.gradeArray = theGradeArray;
}
/***********PART B STARTS HERE**************/
/////////// methods ///////////////
/**
* Method to return the name of this student
* @return the student's name
*/
public String getName() { return this.name; }
/**
* Method to set the name for this student
* @param theName the new name to use
* @return true if success else false
*/
public boolean setName(String theName)
{
boolean result = false;
if (this.name.equals("Joe Student"))
{
this.name = theName;
result = true;
}
return result;
}
/**
* Method to get the grade in the grade array
* at the passed index
* @param index the index that we want the grade for
* @return the grade in the grade array at this passed index
*/
public double getGrade(int index)
{
return this.gradeArray[index];
}
/**
* Method to replace the array of grades
* @param theArray the new array of grades to use
* @return true if sucess, else false
*/
public boolean setGradeArray(double[] theArray)
{
boolean result = false;
if (this.gradeArray == null)
{
this.gradeArray = theArray;
result = true;
}
return result;
}
/**
* Method to set a grade at an index
* @param index the index to set it at
* @param newGrade the grade to use
* @return true if success else false
*/
public boolean setGrade(int index,
double newGrade)
{
boolean result = false;
if (newGrade >= 0 ||
this.gradeArray != null ||
index < this.gradeArray.length ||
index >= 0)
{
this.gradeArray[index] = newGrade;
result = true;
}
return result;
}
/**
* Method to return the average of the grades for this student
* @return the average of the grades or 0.0 if no grade array or
* no grades
*/
public double getAverage()
{
double average = 0.0;
double sum = 0.0;
for (int i = 0; i < this.gradeArray.length; i++)
{
sum = sum + this.gradeArray[i];
}
average = sum / this.gradeArray.length;
return average;
}
/**
* Method to return a string with information about this student
* @return a string with information about the student
*/ public String toString()
{
return "Student object named: " + this.name +
" Average: " + this.getAverage();
}
}
public class Lab9
{
public static void main(String [] args)
{
/**** PART A ****/
Student a = new Student();
System.out.println(a);
Student b = new Student("Shaun White");
System.out.println(b);
double [] d = new double[3];
d[0]=4.0;
d[1]=3.5;
d[2]=2.5;
Student c = new Student("Body Miller", d, "Computer Science");
System.out.println(c);
/**** PART B ****
Student e = new Student();
boolean name = e.setName("Apolo Ohno");
System.out.println("Name was successfully set: " + name);
boolean grade = e.setGrade(0, 4.0);
System.out.println("The first grade was successfully set: " + grade);
boolean major = e.setMajor("Bioengineering");
System.out.println("The major was successfully set: " + major);
System.out.println(e);
System.out.println(e.getName() +
" has changed his mind and realized CS is much better");
major = e.setMajor("Computer Science");
System.out.println(e.getName() + " has a new major! It is " + e.getMajor());
****/
}
}
import java.util.*;
/**
* Class that describes a student. A student has a name and an
* array of grades. You can get information about a student
* such as her/his name and grade average.
*
* @author Barb Ericson [email protected]
*/
public class Student
{
//////////// fields //////////////////
/** the name of the student: first last */
private String name;
/** an array of grades for this student */
private double[] gradeArray;
//////////// constructors ///////////
/**
* No argument constructor. Leaves
* all fields with default values
*/
public Student() {
this.name = "Joe Student";
this.gradeArray = new double[10];
}
/**
* Constructor that takes the name
* @param theName the student's name
*/
public Student(String theName)
{
this.name = theName;
this.gradeArray = new double[10];
}
/**
* Constructor that takes the name
* and an array of grades
* @param theName the student's name
* @param theGradeArray the array of grades
*/
public Student(String theName,
double[] theGradeArray)
{
this.name = theName;
this.gradeArray = theGradeArray;
}
/***********PART B STARTS HERE**************/
/////////// methods ///////////////
/**
* Method to return the name of this student
* @return the student's name
*/
public String getName() { return this.name; }
/**
* Method to set the name for this student
* @param theName the new name to use
* @return true if success else false
*/
public boolean setName(String theName)
{
boolean result = false;
if (this.name.equals("Joe Student"))
{
this.name = theName;
result = true;
}
return result;
}
/**
* Method to get the grade in the grade array
* at the passed index
* @param index the index that we want the grade for
* @return the grade in the grade array at this passed index
*/
public double getGrade(int index)
{
return this.gradeArray[index];
}
/**
* Method to replace the array of grades
* @param theArray the new array of grades to use
* @return true if sucess, else false
*/
public boolean setGradeArray(double[] theArray)
{
boolean result = false;
if (this.gradeArray == null)
{
this.gradeArray = theArray;
result = true;
}
return result;
}
/**
* Method to set a grade at an index
* @param index the index to set it at
* @param newGrade the grade to use
* @return true if success else false
*/
public boolean setGrade(int index,
double newGrade)
{
boolean result = false;
if (newGrade >= 0 ||
this.gradeArray != null ||
index < this.gradeArray.length ||
index >= 0)
{
this.gradeArray[index] = newGrade;
result = true;
}
return result;
}
/**
* Method to return the average of the grades for this student
* @return the average of the grades or 0.0 if no grade array or
* no grades
*/
public double getAverage()
{
double average = 0.0;
double sum = 0.0;
for (int i = 0; i < this.gradeArray.length; i++)
{
sum = sum + this.gradeArray[i];
}
average = sum / this.gradeArray.length;
return average;
}
/**
* Method to return a string with information about this student
* @return a string with information about the student
*/ public String toString()
{
return "Student object named: " + this.name +
" Average: " + this.getAverage();
}
}
public class Lab9
{
public static void main(String [] args)
{
/**** PART A ****/
Student a = new Student();
System.out.println(a);
Student b = new Student("Shaun White");
System.out.println(b);
double [] d = new double[3];
d[0]=4.0;
d[1]=3.5;
d[2]=2.5;
Student c = new Student("Body Miller", d, "Computer Science");
System.out.println(c);
/**** PART B ****
Student e = new Student();
boolean name = e.setName("Apolo Ohno");
System.out.println("Name was successfully set: " + name);
boolean grade = e.setGrade(0, 4.0);
System.out.println("The first grade was successfully set: " + grade);
boolean major = e.setMajor("Bioengineering");
System.out.println("The major was successfully set: " + major);
System.out.println(e);
System.out.println(e.getName() +
" has changed his mind and realized CS is much better");
major = e.setMajor("Computer Science");
System.out.println(e.getName() + " has a new major! It is " + e.getMajor());
****/
}
}