// Client application class and service class Create a NetBeans project named St
ID: 666450 • Letter: #
Question
// Client application class and service class
Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard.
Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard.
After you have created your NetBeans Project your application class StudentClient should contain the following executable code:
package studentclient;
public class StudentClient {
public static void main(String[] args) {
}
}
Your service class should contain the following executable code:
package studentclient;
public class Student {
}
In the StudentClient application class main method code the instructions to perform the tasks indicated in the remarks :
package studentclient;
public class StudentClient{
public static void main( String [] args ){
/* Declare two object references of type Student s1 and s2 and instantiate two Student objects passing three arguments to the constructor for the class. Use different values for each class object */
// Your code here
/* Output the name, social security number and GPA of the student from object reference s1 using the appropriate accessor methods to obtain the data */
// Your code here
/* Output the name, social security number and GPA of the student from object reference s2 using the toString method to return the data */
// Your code here
/* Using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */
// Your code here
/* Using the appropriate mutator methods on student object s2, change the name, social security number and GPA to the same values as in object s1. Use the set methods. */
// Your code here
/* Again, using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */
// Your code here
}
}
In the Student service class code the instructions to define your Student service class structure with the appropriate constructors, mutator methods, accessor methods as well as a toString and an equals method :
package studentclient;
public class Student
{
/* Declare three instance variables to represent the student name, social security number and GPA */
// Your code here
/* Overloaded constructor method:
Allows client to set beginning values for name, ssn, and gpa. This constructor takes three parameters and calls mutator methods to validate new values */
public Student( String newName, String newSsn, double newGpa )
{
// Your code here
}
/* getName accessor method */
public String getName( )
{
// Your code here
}
/* setName mutator method */
public void setName( String newName )
{
// Your code here } /* getSsn accessor method */
public String getSsn( )
{
// Your code here
}
/* setSSN mutator method */
public void setSsn( String newSsn )
{
// Your code here
}
/* getGpa accessor method */
public double getGpa( )
{
// Your code here
}
/* setGpa mutator method: Allows client to set value of gpa and prints an error message if new value is either less than 0 or greater than 4.0. setGpa does not change the value of gpa if newGpa is negative or greater than 4.0 */
public void setGpa( double newGpa )
{
// Your code here
}
/* toString method returns student name, social security number and GPA */
public String toString( )
{
// Your code here
}
/* equals method returns boolean
Compares two Student objects for the same field values returns a boolean, true if this object has the same field value as the parameter object */
public boolean equals( Object o )
{
// Your code here
}
}
When your application has been successfully compiled and executed, Zip the StudentClient project folder. After the StudentClient project folder has been successfully compressed, upload the zipped folder to your Blackboard account for Programming Exercise 3.
Explanation / Answer
//Program:
package studentclient;
import java.util.Scanner;
public class StudentClient {
public static void main(String[] args) {
String n;
String Sn;
double g;
System.out.println("Enter Student 1 details");
System.out.println("Name:");
Scanner sc=new Scanner(System.in);
n=sc.next();
System.out.println("Social Security number");
Sn=sc.next();
System.out.println("GPA");
g=sc.nextDouble();
Student s1=new Student(n,Sn,g);
System.out.println("Enter Student 2 details");
System.out.println("Name:");
n=sc.next();
System.out.println("Social Security number");
Sn=sc.next();
System.out.println("GPA");
g=sc.nextDouble();
Student s2=new Student(n,Sn,g);
/* Output the name, social security number and GPA of the student from object reference s1
using the appropriate accessor methods to obtain the data */
// Your code here
System.out.println("Student 1 details");
System.out.println("name:"+s1.getName());
System.out.println("Social Security Number:"+s1.getSsn());
System.out.println("GPA:"+s1.getGpa());
/* Output the name, social security number and GPA of the student from object reference s2
using the toString method to return the data */
// Your code here
System.out.println("Student 2 details");
System.out.println("name:"+s2.getName());
System.out.println("Social Security Number:"+s2.getSsn());
System.out.println("GPA:"+s2.getGpa());
/* Using the equals method and a selection control structure (if statement), compare objects
s1 and s2 and output an appropriate message indicating if the objects are equal */
// Your code here
if(s1.equals(s2))
System.out.println("Student details are Equal");
else
System.out.println("Student details are not Equal");
/* Using the appropriate mutator methods on student object s2, change the name, social security
number and GPA to the same values as in object s1. Use the set methods. */
// Your code here
System.out.println("Enter new details for Student 2");
System.out.println("Name:");
n=sc.next();
s2.setName(n);
System.out.println("Social Security number");
Sn=sc.next();
s2.setSsn(Sn);
System.out.println("GPA");
g=sc.nextDouble();
s2.setGpa(g);
/* Again, using the equals method and a selection control structure (if statement),
compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */
// Your code here
if(s1.equals(s2))
System.out.println("Student details are Equal");
else
System.out.println("Student details are not Equal");
}
}
class Student
{
String name;
String social_security_number;
double GPA;
public Student( String newName, String newSsn, double newGpa )
{
this.name=newName;
this.social_security_number=newSsn;
this.GPA=newGpa;
}
public String getName( )
{
return this.name;
}
/* setName mutator method */
public void setName( String newName )
{
// Your code here
this.name=newName;
}
/* getSsn accessor method */
public String getSsn( )
{
// Your code here
return this.social_security_number;
}
/* setSSN mutator method */
public void setSsn( String newSsn )
{
// Your code here
this.social_security_number=newSsn;
}
/* getGpa accessor method */
public double getGpa( )
{
// Your code here
return this.GPA;
}
/* setGpa mutator method: Allows client to set value of gpa and prints an error message
if new value is either less than 0 or greater than 4.0. setGpa does not change the value
of gpa if newGpa is negative or greater than 4.0 */
public void setGpa( double newGpa )
{
// Your code here
if(newGpa<0 || newGpa>4)
System.out.println("Error! Gpa value should be in the range(0 to 4 ) only.");
else
this.GPA=newGpa;
}
/* toString method returns student name, social security number and GPA */
@Override
public String toString( )
{
// Your code here
return ("name:"+this.name+" Social Security number:"+this.social_security_number+ " GPA:"+this.GPA);
}
/* equals method returns boolean
Compares two Student objects for the same field values returns a boolean, true if this object has
the same field value as the parameter object */
public boolean equals( Student S )
{
// Your code here
boolean ans=false;
if(this.name.equals(S.name) && this.social_security_number.equals(S.social_security_number)
&&this.GPA==S.GPA)
{
ans=true;
}
return ans;
}
}
//Output:
run:
Enter Student 1 details
Name:
Bala
Social Security number
1234
GPA
2.3
Enter Student 2 details
Name:
David
Social Security number
3456
GPA
2.3
Student 1 details
name:Bala
Social Security Number:1234
GPA:2.3
Student 2 details
name:David
Social Security Number:3456
GPA:2.3
Student details are not Equal
Enter new details for Student 2
Name:
Bala
Social Security number
1234
GPA
5
Error! Gpa value should be in the range(0 to 4 ) only.
Student details are Equal
BUILD SUCCESSFUL (total time: 44 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.