Can someone help me with this code? When I run it, it stops after the output
Enter Student 1 Details
Name
I would just like to know what is incorrect with my code & how I can fix it.
Here is my code:
package studentclient;
import java.util.Scanner;
public class StudentClient {
public static void main(String[] args) {
Student s1;
Student s2;
s1 = new Student("Lisa", "111-11-111", 3.0);
s2 = new Student("Jacob", "777-77-777", 4.0);
System.out.println("Enter Student 1 Details");
System.out.println("Name");
Scanner sc=new Scanner(System.in);
String Name = sc.next();
System.out.println("Social Security number");
String Ssn = sc.next();
System.out.println("GPA");
double gpa = sc.nextDouble();
System.out.println("Enter Student 2 Details");
System.out.println("Name");
Name=sc.next();
System.out.println("social security number");
Ssn=sc.next();
System.out.println("GPA");
gpa=sc.nextDouble();
System.out.println("Student 1 deatils");
System.out.println("name"+s1.getName());
System.out.println("Social Secuirty Number:"+s1.getSsn());
System.out.println("GPA:"+s1.getGpa());
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());
if(s1 ==(s2))
System.out.println("Student details are equal");
else
System.out.println("Student details are not equal");
s2 = new Student("Jacob", "000-00-000", 4.0);
System.out.println("Enter new details for Student 2");
System.out.println("Name:");
Name=sc.next();
s2.setName();
System.out.println("Social Security number");
Ssn=sc.next();
s2.setSsn(Ssn);
System.out.println("GPA");
gpa=sc.nextDouble();
s2.setGpa(gpa);
if(s1 ==(s2))
System.out.println("Student details are equal");
else
System.out.println("Student details are not equal");
}
}
package studentclient;
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;
}
public void setName( String newName )
{
this.name=newName;
}
public String getSsn( )
{
return this.social_security_number;
}
public void setSsn( String newSsn )
{
// Your code here
this.social_security_number=newSsn;
}
public double getGpa( )
{
return this.GPA;
}
public void setGpa( double newGpa )
{
if(newGpa<0 || newGpa>4)
System.out.println("Error! Gpa value should be in the range(0 to 4 ) only.");
else
this.GPA=newGpa;
}
@Override
public String toString( )
{
return ("name:"+this.name+" Social Security number:"+this.social_security_number+ "
GPA:"+this.GPA);
}
public boolean equals( Student S )
{
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;
}
void setName() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of
generated methods, choose Tools | Templates.
}
}
Finally, here are the isntructions for the project:
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 classStudentClient 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 Studentservice 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 */
publicStudent( String newName, String newSsn, double newGpa )
{
// Your code here
}
/* getName accessor method */
publicString getName( )
{
// Your code here
}
/* setName mutator method */
publicvoid setName( String newName )
{
// Your code here } /* getSsn accessor method */
publicString getSsn( )
{
// Your code here
}
/* setSSN mutator method */
publicvoid setSsn( String newSsn )
{
// Your code here
}
/* getGpa accessor method */
publicdouble 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 */
publicvoid setGpa( double newGpa )
{
// Your code here
}
/* toString method returns student name, social security number and GPA */
publicString 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 */
publicboolean equals( Object o )
{
// Your code here
}
}
Solution
Student.java
package studentclient;
public class Student {
//Declaring Instance variables.
private String name;
private String social_security_number;
private double GPA;
/*
* Parameterized constructor
* @param name,Social_security_number,GPA
*/
public Student(String name, String social_security_number, double gPA) {
super();
this.name = name;
this.social_security_number = social_security_number;
GPA = gPA;
}
//Setters and Getters.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSocial_security_number() {
return social_security_number;
}
public void setSocial_security_number(String social_security_number) {
this.social_security_number = social_security_number;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
if(gPA<0 || gPA>4)
{
System.out.println(":: GPA must be greater than or equal to zero and less than 4 ::");
}
GPA = gPA;
}
/*
* toString() method is used to display the contents of an object.
* @Param No param
* @Return null
*/
@Override
public String toString() {
System.out.println("Name:"+getName());
System.out.println("Social Security Number:"+getSocial_security_number());
System.out.println("GPA:"+getGPA());
return " ";
}
/*
* This Method compares two Objects
* @Param Object
* @Return Boolean Value.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (Double.doubleToLongBits(GPA) != Double.doubleToLongBits(other.GPA))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (social_security_number == null) {
if (other.social_security_number != null)
return false;
} else if (!social_security_number.equals(other.social_security_number))
return false;
return true;
}
}
________________________________________________________________________
StudentClient.java
package studentclient;
import java.util.Scanner;
public class StudentClient {
public static void main(String[] args) {
//Scanner Object is used to read the values entered by the user.
Scanner sc = new Scanner(System.in);
/*
* Declaring the Student class reference variables.
*/
Student s1 = null;
Student s2 = null;
//Getting Student::1 Details from the user.
System.out.println("======Enter Student 1 Details======");
System.out.print("Name:");
String name = sc.next();
System.out.print("Social Security number:");
String ssn = sc.next();
System.out.print("GPA:");
double gpa = sc.nextDouble();
//Getting Student::2 Details from the user.
System.out.println("======Enter Student 2 details======");
System.out.print("Name:");
String name1 = sc.next();
System.out.print("Social Security number:");
String ssn1 = sc.next();
System.out.print("GPA:");
double gpa1 = sc.nextDouble();
/*
* Creating Student::1 class Object
* By passing name,SocialSecurityNumber,GPA arguments
*/
s1 = new Student(name, ssn, gpa);
/*
* Creating Student::2 class Object
* By passing name,SocialSecurityNumber,GPA arguments
*/
s2 = new Student(name1, ssn1, gpa1);
//Displaying the Student::1 Details.
System.out.println("========= Student 1 details =========");
System.out.println("Name:" + s1.getName());
System.out.println("Social Secuirty Number:"+ s1.getSocial_security_number());
System.out.println("GPA:" + s1.getGPA());
//Displaying the Student::2 Details by using toString() Method.
System.out.println("========= Student 2 details =========");
s2.toString();
/*
* Comparing the Student::1 and Student::2 Objects
* If those are not equal change the instance variable values of Student::2 object
* By using Mutator methods.After Mutating once again compare the Objects.
*/
while(true)
{
if (s1.equals(s2))
{
System.out.println("::Student::1 And Student::2 Objects are equal ::");
break;
}
else
{
System.out.println("Using appropriate Mutator methods changing the instance
variable values of Student::2 Object");
s2.setName(s1.getName());
s2.setSocial_security_number(s1.getSocial_security_number());
s2.setGPA(s1.getGPA());
System.out.println(" ");
System.out.println("After Mutating Student::2 Object");
continue;
}
}
}
}
____________________________________________________________________________
Output:
======Enter Student 1 Details======
Name:Lisa
Social Security number:111-11-111
GPA:3.0
======Enter Student 2 details======
Name:Jacob
Social Security number:777-77-777
GPA:4.0
========= Student 1 details =========
Name:Lisa
Social Secuirty Number:111-11-111
GPA:3.0
========= Student 2 details =========
Name:Jacob
Social Security Number:777-77-777
GPA:4.0
Using appropriate Mutator methods
changing the instance variable values of Student::2 Object
::Student::1 And Student::2 Objects are equal ::
_____________________________________________________________________________

Can someone help me with this code When I run it, it stops after th.pdf

  • 1.
    Can someone helpme with this code? When I run it, it stops after the output Enter Student 1 Details Name I would just like to know what is incorrect with my code & how I can fix it. Here is my code: package studentclient; import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1; Student s2; s1 = new Student("Lisa", "111-11-111", 3.0); s2 = new Student("Jacob", "777-77-777", 4.0); System.out.println("Enter Student 1 Details"); System.out.println("Name"); Scanner sc=new Scanner(System.in); String Name = sc.next(); System.out.println("Social Security number"); String Ssn = sc.next(); System.out.println("GPA"); double gpa = sc.nextDouble(); System.out.println("Enter Student 2 Details"); System.out.println("Name"); Name=sc.next(); System.out.println("social security number"); Ssn=sc.next(); System.out.println("GPA"); gpa=sc.nextDouble();
  • 2.
    System.out.println("Student 1 deatils"); System.out.println("name"+s1.getName()); System.out.println("SocialSecuirty Number:"+s1.getSsn()); System.out.println("GPA:"+s1.getGpa()); 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()); if(s1 ==(s2)) System.out.println("Student details are equal"); else System.out.println("Student details are not equal"); s2 = new Student("Jacob", "000-00-000", 4.0); System.out.println("Enter new details for Student 2"); System.out.println("Name:"); Name=sc.next(); s2.setName(); System.out.println("Social Security number"); Ssn=sc.next(); s2.setSsn(Ssn); System.out.println("GPA"); gpa=sc.nextDouble(); s2.setGpa(gpa); if(s1 ==(s2)) System.out.println("Student details are equal"); else System.out.println("Student details are not equal");
  • 3.
    } } package studentclient; 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; } public void setName( String newName ) { this.name=newName; } public String getSsn( ) { return this.social_security_number; } public void setSsn( String newSsn ) { // Your code here this.social_security_number=newSsn; }
  • 4.
    public double getGpa() { return this.GPA; } public void setGpa( double newGpa ) { if(newGpa<0 || newGpa>4) System.out.println("Error! Gpa value should be in the range(0 to 4 ) only."); else this.GPA=newGpa; } @Override public String toString( ) { return ("name:"+this.name+" Social Security number:"+this.social_security_number+ " GPA:"+this.GPA); } public boolean equals( Student S ) { 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; } void setName() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } } Finally, here are the isntructions for the project: Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on
  • 5.
    Blackboard. Add a classnamed 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 classStudentClient 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
  • 6.
    number and GPAto 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 Studentservice 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 */ publicStudent( String newName, String newSsn, double newGpa ) { // Your code here } /* getName accessor method */ publicString getName( ) { // Your code here } /* setName mutator method */ publicvoid setName( String newName ) { // Your code here } /* getSsn accessor method */
  • 7.
    publicString getSsn( ) { //Your code here } /* setSSN mutator method */ publicvoid setSsn( String newSsn ) { // Your code here } /* getGpa accessor method */ publicdouble 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 */ publicvoid setGpa( double newGpa ) { // Your code here } /* toString method returns student name, social security number and GPA */ publicString 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 */ publicboolean equals( Object o ) { // Your code here } }
  • 8.
    Solution Student.java package studentclient; public classStudent { //Declaring Instance variables. private String name; private String social_security_number; private double GPA; /* * Parameterized constructor * @param name,Social_security_number,GPA */ public Student(String name, String social_security_number, double gPA) { super(); this.name = name; this.social_security_number = social_security_number; GPA = gPA; } //Setters and Getters. public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSocial_security_number() { return social_security_number; } public void setSocial_security_number(String social_security_number) { this.social_security_number = social_security_number; } public double getGPA() { return GPA; } public void setGPA(double gPA) {
  • 9.
    if(gPA<0 || gPA>4) { System.out.println("::GPA must be greater than or equal to zero and less than 4 ::"); } GPA = gPA; } /* * toString() method is used to display the contents of an object. * @Param No param * @Return null */ @Override public String toString() { System.out.println("Name:"+getName()); System.out.println("Social Security Number:"+getSocial_security_number()); System.out.println("GPA:"+getGPA()); return " "; } /* * This Method compares two Objects * @Param Object * @Return Boolean Value. */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (Double.doubleToLongBits(GPA) != Double.doubleToLongBits(other.GPA)) return false; if (name == null) {
  • 10.
    if (other.name !=null) return false; } else if (!name.equals(other.name)) return false; if (social_security_number == null) { if (other.social_security_number != null) return false; } else if (!social_security_number.equals(other.social_security_number)) return false; return true; } } ________________________________________________________________________ StudentClient.java package studentclient; import java.util.Scanner; public class StudentClient { public static void main(String[] args) { //Scanner Object is used to read the values entered by the user. Scanner sc = new Scanner(System.in); /* * Declaring the Student class reference variables. */ Student s1 = null; Student s2 = null; //Getting Student::1 Details from the user. System.out.println("======Enter Student 1 Details======"); System.out.print("Name:"); String name = sc.next(); System.out.print("Social Security number:"); String ssn = sc.next(); System.out.print("GPA:"); double gpa = sc.nextDouble(); //Getting Student::2 Details from the user.
  • 11.
    System.out.println("======Enter Student 2details======"); System.out.print("Name:"); String name1 = sc.next(); System.out.print("Social Security number:"); String ssn1 = sc.next(); System.out.print("GPA:"); double gpa1 = sc.nextDouble(); /* * Creating Student::1 class Object * By passing name,SocialSecurityNumber,GPA arguments */ s1 = new Student(name, ssn, gpa); /* * Creating Student::2 class Object * By passing name,SocialSecurityNumber,GPA arguments */ s2 = new Student(name1, ssn1, gpa1); //Displaying the Student::1 Details. System.out.println("========= Student 1 details ========="); System.out.println("Name:" + s1.getName()); System.out.println("Social Secuirty Number:"+ s1.getSocial_security_number()); System.out.println("GPA:" + s1.getGPA()); //Displaying the Student::2 Details by using toString() Method. System.out.println("========= Student 2 details ========="); s2.toString(); /* * Comparing the Student::1 and Student::2 Objects * If those are not equal change the instance variable values of Student::2 object * By using Mutator methods.After Mutating once again compare the Objects. */ while(true) { if (s1.equals(s2))
  • 12.
    { System.out.println("::Student::1 And Student::2Objects are equal ::"); break; } else { System.out.println("Using appropriate Mutator methods changing the instance variable values of Student::2 Object"); s2.setName(s1.getName()); s2.setSocial_security_number(s1.getSocial_security_number()); s2.setGPA(s1.getGPA()); System.out.println(" "); System.out.println("After Mutating Student::2 Object"); continue; } } } } ____________________________________________________________________________ Output: ======Enter Student 1 Details====== Name:Lisa Social Security number:111-11-111 GPA:3.0 ======Enter Student 2 details====== Name:Jacob Social Security number:777-77-777 GPA:4.0 ========= Student 1 details ========= Name:Lisa Social Secuirty Number:111-11-111 GPA:3.0 ========= Student 2 details ========= Name:Jacob Social Security Number:777-77-777 GPA:4.0
  • 13.
    Using appropriate Mutatormethods changing the instance variable values of Student::2 Object ::Student::1 And Student::2 Objects are equal :: _____________________________________________________________________________