Assignment6 – MathStudent CS1405 Spring2011 Description: Create a project called
ID: 3623547 • Letter: A
Question
Assignment6 – MathStudent CS1405 Spring2011
Description:
Create a project called MathStudent with two java files called MathStudent.java and MathStudent Test.java
As always include a comment with your name, course number, and assignment number on top of each source code file.
MathStudent.java:
Implement the MathStudent class with the class members described in the UML diagram:
MathStudent
-firstName: String
-lastName: String
-gpa : double
<<constructor>>MathStudent (fName:String, lName:String,gpaValue:Double)
+getGpa():Double
+setGpa(gpaValue:Double)
+countBySeven(max:int)
+toString():String
Do not add, remove, or modify any class members. I want to see that you can read a UML class diagram and implement your class based on the descriptions you find there.
If you are unsure how the class diagram translates to field and method definitions read the paragraph “UML Class Diagram for Class Account” on page 95. If that does not answer your questions email me and I’ll help you clarify.
Ad gpa:
Ensure a valid gpa between 0 and 4.0 (inclusive).
If the gpaValue argument is less than 0 print a message that the gpa cannot be less than 0.
If the gpaValue argument is greater than 4.0 print a message that the gpa cannot be greater than 4.0
Otherwise assign the gpaValue to the private field gpa
You need to ensure a valid gpa argument both in the constructor and in the set accessor, however, you should follow the DRY principle (Don’t repeat yourself) You can accomplish that by checking for a correct gpa value in the set accessor and then using the set accessor in the constructor (rather than assigning the private gpa field directly in the contructor).
Ad countBySeven:
The method countBySeven prints out all the multiples of seven that are less than or equal to the argument value max.
If max is 50 the methods prints out: 7 14 21 28 35 42 49
If max is less than 7 the methods prints out nothing
Ad toString:
Every class comes with a default implementation of the instance method toString. The method toString has the purpose to provide a string representation of an individual class instance. However, the default implementation doesn’t provide meaningful information.
Because of that it is good practice to override toString. Typically this is done by providing the values of data (fields) that are characteristic / significant for a class instance.
Since the class MathStudent has only three fields we will provide the values of all the data (fields). That will allow us to distinguish between different class objects and to track changes.
Adding the annotation @Override above the method implementation tells the compiler as well as the reader of your method that you change an existing implementation to make it more appropriate for your class. It is recommended to add a @Override annotation above every method that overrides an existing one.
@Override
public String toString()
{
// TODO: write code that returns a String representing your object
}
MathStudentTest.java:
This assignment does not require you to gather input from the user. You may hard-code the argument values in the method / constructor calls. However, if you prefer to read in the data values feel free to do so.
• Test your MathStudent class by creating an instance (object) of type MathStudent
• Print out the string created by the toString method
• Change the gpa to -3
• Again print out the string representing your MathStudent object
• Change the gpa to a valid value other than the initial value set in the constructor
• Print out the string representing your MathStudent object
• Change the gpa to 4.2
• Once again print out the string representing your MathStudent object
• Call countBySeven with a max value of -1
• Call countBySeven with a max value of 100
Sample output:
MathStudent Test:
Ï
Emily Johnson .. gpa:3.6
gpa can't be less than 0
Emily Johnson .. gpa:3.6
Emily Johnson .. gpa:3.8
gpa can't be higher than 4.0
Emily Johnson .. gpa:3.8
7 14 21 28 35 42 49 56 63 70 77 84 91 98
Turning in:
Zip up your project (the directory containing the source code and the project files) and rename the file
A6_YourName.zip . Turn it in via Virtual Campus.
Maximum Points: 30 pts
• 7 points each for setGpa and countBySeven 10
• 5 points for the toString method
• 4 points for the constructor
• 2 points for the private fields
• 5 points for the test class
Explanation / Answer
please rate - thanks
public class MathStudentTest
{public static void main(String[] args)
{MathStudent s=new MathStudent("Emily","Johnson",3.6);
System.out.println(s.toString());
s.setGpa(-3);
System.out.println(s.toString());
s.setGpa(3.8);
System.out.println(s.toString());
s.setGpa(4.2);
System.out.println(s.toString());
s.countBySeven(-1);
s.countBySeven(100);
}
-----------------------------------------------------
public class MathStudent
{private String firstName,lastName;
private double gpa;
public MathStudent(String fName, String lName, double gpaValue)
{firstName=fName;
lastName=lName;
setGpa(gpaValue);
}
public double getGpa()
{return gpa;
}
public void setGpa(double gpaValue)
{if(gpaValue<0)
System.out.println("gpa can't be less than 0");
else if(gpaValue>4)
System.out.println("gpa can't be higher than 4.0");
else
gpa=gpaValue;
}
public void countBySeven(int max)
{int i;
if(max>=7)
{for(i=7;i<=max;i+=7)
System.out.print(i+" ");
System.out.println();
}
}
@Override
public String toString()
{String buffer=firstName+" "+lastName+"..gpa:"+gpa;
return buffer;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.