Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class StudentClient { public static void main(S

ID: 3860105 • Letter: I

Question

import java.util.Scanner;

public class StudentClient {

  

   public static void main(String[] args)
   {
       Student s1 = new Student();
        Student s2 = new Student("Smith", "123-45-6789", 3.2);
        Student s3 = new Student("Jones", "987-65-4321", 3.7);
        System.out.println("The name of student #1 is ");
        System.out.println("The social security number of student #1 is " + s1.toString());
        System.out.println("Student #2 is " + s2);
        System.out.println("the name of student #3 is " + s3.getName());
        System.out.println("The social security number of student is " + s3.getSsn());
        System.out.println("The gpa of student #3 is " + s3.getGpa());
        System.out.println("Student #3 is " + s3.toString());
      
        s3.setName("Adam");
        s3.setSsn("555-55-5555");
        s3.setGpa(4.0);
        System.out.println("Here is student #3 after the changes: " + s3.toString());
        System.out.println("Now set the gpa of student #2 to 5.0 " );
        s2.setGpa(5.0);
        System.out.println(" " + "student #2 is " + s2.toString());
        System.out.println("There are students ");
      
      
   }

import java.util.Scanner;
public class Student
{
   
        private String name;
        private String ssn;
        private double gpa;
        private static int studentCount = 0;
        public Student()
        {
        name = "";
        ssn = "";
        gpa = 0.0;
        studentCount++;
        }
       
        public Student(String newName, String newSsn, double newGpa)
        {
            name = newName;
            ssn = newSsn;
            gpa = newGpa;
            studentCount++;
        }
       
        public String getName()
        {
            return name;
        }
       
        public void setName(String newName)
        {
           
            name = newName;
           
        }
       
        public String getSsn()
        {
            return ssn;
        }
       
        public void setSsn(String newSsn)
        {
            ssn = newSsn;
        }
       
        public double getGpa()
        {
            return gpa;
        }
       
        public void setGpa(double newGpa)
        {
           
            if(newGpa > 0.0 && newGpa <= 4.0)
            gpa = newGpa;
            else
            {
                System.err.println("GPA must be betweeb 0.0 and 4.0. Value not changed.");   
            }
        }
       
       
        public static int getCount()
        {
            return getCount();
        }
       
        public String toString()
        {
            return("Name: " + name + " SSN: " + ssn + " GPA: " + gpa);
        }
       
       

Use a getcount() method to get the number of students ?

Explanation / Answer

Hi,

I have modified the code and highlighted the code changes below.

StudentClient.java


import java.util.Scanner;
public class StudentClient {
  
public static void main(String[] args)
{
Student s1 = new Student();
Student s2 = new Student("Smith", "123-45-6789", 3.2);
Student s3 = new Student("Jones", "987-65-4321", 3.7);
System.out.println("The name of student #1 is ");
System.out.println("The social security number of student #1 is " + s1.toString());
System.out.println("Student #2 is " + s2);
System.out.println("the name of student #3 is " + s3.getName());
System.out.println("The social security number of student is " + s3.getSsn());
System.out.println("The gpa of student #3 is " + s3.getGpa());
System.out.println("Student #3 is " + s3.toString());
  
s3.setName("Adam");
s3.setSsn("555-55-5555");
s3.setGpa(4.0);
System.out.println("Here is student #3 after the changes: " + s3.toString());
System.out.println("Now set the gpa of student #2 to 5.0 " );
s2.setGpa(5.0);
System.out.println(" " + "student #2 is " + s2.toString());
System.out.println("There are students ");
System.out.println("Number of studnets: "+Student.getCount());
  
}
}

Student.java


import java.util.Scanner;
public class Student
{

private String name;
private String ssn;
private double gpa;
private static int studentCount = 0;
public Student()
{
name = "";
ssn = "";
gpa = 0.0;
studentCount++;
}

public Student(String newName, String newSsn, double newGpa)
{
name = newName;
ssn = newSsn;
gpa = newGpa;
studentCount++;
}

public String getName()
{
return name;
}

public void setName(String newName)
{

name = newName;

}

public String getSsn()
{
return ssn;
}

public void setSsn(String newSsn)
{
ssn = newSsn;
}

public double getGpa()
{
return gpa;
}

public void setGpa(double newGpa)
{

if(newGpa > 0.0 && newGpa <= 4.0)
gpa = newGpa;
else
{
System.err.println("GPA must be betweeb 0.0 and 4.0. Value not changed.");   
}
}


public static int getCount()
{
return studentCount;
}

public String toString()
{
return("Name: " + name + " SSN: " + ssn + " GPA: " + gpa);
}

}

Output:

The name of student #1 is
The social security number of student #1 is Name: SSN: GPA: 0.0
Student #2 is Name: Smith SSN: 123-45-6789 GPA: 3.2
the name of student #3 is Jones
The social security number of student is 987-65-4321
The gpa of student #3 is 3.7
Student #3 is Name: Jones SSN: 987-65-4321 GPA: 3.7
Here is student #3 after the changes: Name: Adam SSN: 555-55-5555 GPA: 4.0
Now set the gpa of student #2 to 5.0

student #2 is Name: Smith SSN: 123-45-6789 GPA: 3.2
There are students
Number of studnets: 3
GPA must be betweeb 0.0 and 4.0. Value not changed.