Question

java This lab is intended to give you practice creating a class with a constructor method,...

java 


This lab is intended to give you practice creating a class with 
a constructor method, accessor methods, mutator methods, equals
method , toString method and a equals method.

In this lab you need to create two separate classes, one Student class and other Lab10 class.
You need to define your instance variables, accessor methods, mutator methods, constructor,
toString method and equals method in Student class.

You need to create objects in Lab10 class which will have your main method and need to add various 
functionalities so that you can get the output as shown in sample output.

//  Name:
//  Section:

//  Lab 10
//  CS1113
//  Fall 2016
// Class Lab10


public class Lab10
{ 
  //Constants
   private final static String NAME = "YOUR NAME"; // replace YOUR NAME with your name
   private final static int STUID = 123456789;
   private final static double GPA1  = 4.00;
   private final static double GPA2  = 2.34;

  //main method
   public static void main (String[] args)
   {
      Student stu1;

      stu1 = new Student(NAME, STUID, GPA1); 

      System.out.println("\nName: " + stu1.getName());
      System.out.println("Id Number: " + stu1.getIdNum());
      System.out.println("GPA : " + stu1.getGPA());
      stu1.setGPA(GPA2);
      System.out.println(stu1 + "\n");

      // Create a second student object
      // With a name of Trine Thunder, a
      // gpa of 4.00, and a student Id of
      // 000000001
      // You do not need to declare these at final constants,
      // but you can if you want to.

      // [Add code here]

      // Print out this student similar to above.
      // You do not need to reset the gpa of this student.

      // [Add code here]
          
          // Check if both objects are same using == and .equals() methods.
      // Print the message in both cases, whether same or not as shown in sample output.

      // [Add code here]

   } // end of main
} // end of class Lab10


//  Name:
//  Section:

//  Lab 10
//  CS1113
//  Fall 2016
//  Class : Student.java

public class Student
{
   //class variables
   private String name;
   private int idNum;
   private double gpa;

   // Constructor
   public Student(String namePar, int idNumPar, double gpaPar)
   {
      // Save namePar to class variable name

      // [Add code here]

      // Save idNumPar to class variable idNum

      // [Add code here]

      // Save gpaPar to class variable gpa

      // [Add code here]

   }

   // Accessor: returns name of student
   public String getName()
   {
      // Return the name of the student 

      // [Add code here]
   }

   // Accessor: returns GPA of student
   public double getGPA()
   {
      // Return the gpa of the student

      // [Add code here]
   }

   // Accessor: returns idNum of student
   public int getIdNum()
   {
      // Return the idnum of the Student

      // [Add code here]
   }


   // Mutator: Changes the GPA of the student
   public void setGPA(double g)
   {
      // Set the class variable gpa equal to g

      // [Add code here]
   }

   // toString method: Returns a formatted string
   //  of student information.
   // As shown in sample output.
   public String toString()
   {
      // declare a String variable to store the string value
          // then return the formatted String.

      // [Add code here]
   }
   
   // implement .equals() method which returns a boolean value
   //  of student information. When you call this method, 
   // it should print message as shown in sample output.
   public boolean equals(Student s)
   {
                //[Add code here]
   }
} // end of class Student

 
1) Fill in the needed code in the file (class) 
   Student.java (Student) and Lab10.java. 
   The program will not compile until you do so.  
   You will have 2 files, one named Lab10.java 
   and one named Student.java. 

   Compile the subsidiary class, Student.java,
   first, using

   javac Student.java

   Once you have eliminated any and all syntax errors
   in the student class.  Then compile

   javac Lab10.java

   and eliminate any syntax errors from that class.
   Then you can run the program using

   java Lab10

2) Run the program and redirect the output to Lab10_out.txt

A Sample output is below:

Name: John Terry
Id Number: 123456789
GPA : 4.0
Student Name: John Terry
Student Id num:123456789
Student GPA: 2.34


Name: Trine Thunder
Id Number: 1
GPA : 4.0
Student Name: Trine Thunder
Student Id num:1
Student GPA: 4.0

Using == .....
Both are different.

Using .equals() method.....
Both are different.

3) Now change your code so that you pass same values for both stu1 and stu2.
   Compile and run your program.
   Your output should look like:
   
Name: John Terry
Id Number: 123456789
GPA : 4.0
Student Name: John Terry
Student Id num:123456789
Student GPA: 2.34


Name: Trine Thunder
Id Number: 1
GPA : 4.0
Student Name: Trine Thunder
Student Id num:1
Student GPA: 4.0

Using == .....
Both are different.

Using .equals() method.....
Both are different.

Explain why the result is not as expected?

Revert back to your previous version of your java code.

4)  Turn in a printed copy of both Student.java and Lab10.java.  Also
    turn in a printed copy of your output file created in step 2.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lab10.java


public class Lab10
{
//Constants
private final static String NAME = "John Terry"; // replace YOUR NAME with your name
private final static int STUID = 123456789;
private final static double GPA1 = 4.00;
private final static double GPA2 = 2.34;

//main method
public static void main (String[] args)
{
Student stu1;

stu1 = new Student(NAME, STUID, GPA1);

System.out.println("\nName: " + stu1.getName());
System.out.println("Id Number: " + stu1.getIdNum());
System.out.println("GPA : " + stu1.getGPA());
stu1.setGPA(GPA2);
System.out.println(stu1 + "\n");

// Create a second student object
// With a name of Trine Thunder, a
// gpa of 4.00, and a student Id of
// 000000001
// You do not need to declare these at final constants,
// but you can if you want to.
System.out.println("\nName: Trine Thunder" );
System.out.println("Id Number: 000000001");
System.out.println("GPA : 4.00");
Student stu2 = new Student("Trine Thunder",000000001, 4.00 );
// [Add code here]
  
// Print out this student similar to above.
// You do not need to reset the gpa of this student.
System.out.println(stu2 + "\n");

// [Add code here]
  
// Check if both objects are same using == and .equals() methods.
// Print the message in both cases, whether same or not as shown in sample output.
System.out.println("Using ==");
   if(stu1 == stu2){
       System.out.println("Both are same");
   }
   else{
       System.out.println("Both are different");
   }
   stu1.equals(stu2);
// [Add code here]

} // end of main
} // end of class Lab10


// Name:
// Section:

// Lab 10
// CS1113
// Fall 2016
// Class : Student.java

public class Student
{
//class variables
private String name;
private int idNum;
private double gpa;

// Constructor
public Student(String namePar, int idNumPar, double gpaPar)
{
// Save namePar to class variable name
   name = namePar;
// [Add code here]
   idNum = idNumPar;
// Save idNumPar to class variable idNum
   gpa = gpaPar ;
// [Add code here]

// Save gpaPar to class variable gpa

// [Add code here]

}

// Accessor: returns name of student
public String getName()
{
// Return the name of the student
   return name;
// [Add code here]
}

// Accessor: returns GPA of student
public double getGPA()
{
// Return the gpa of the student
   return gpa;
// [Add code here]
}

// Accessor: returns idNum of student
public int getIdNum()
{
// Return the idnum of the Student
   return idNum;
// [Add code here]
}


// Mutator: Changes the GPA of the student
public void setGPA(double g)
{
// Set the class variable gpa equal to g
   gpa = g;
// [Add code here]
}

// toString method: Returns a formatted string
// of student information.
// As shown in sample output.
public String toString()
{
// declare a String variable to store the string value
// then return the formatted String.
   String s ="";
   s = s + "Student Name: "+name+"\n";
   s =s + "Student Id num: "+idNum+"\n";
   s =s + "Student GPA: "+gpa+"\n";
   return s;
// [Add code here]
}

// implement .equals() method which returns a boolean value
// of student information. When you call this method,
// it should print message as shown in sample output.
public boolean equals(Student s)
{
//[Add code here]
   System.out.println("Using .equals() method");
   if(this.equals(s.idNum == this.idNum)){
       System.out.println("Both are same");
       return true;
   }
   else{
       System.out.println("Both are different");
       return false;
   }
     
     
}
} // end of class Student

Output:


Name: John Terry
Id Number: 123456789
GPA : 4.0
Student Name: John Terry
Student Id num: 123456789
Student GPA: 2.34

Name: Trine Thunder
Id Number: 000000001
GPA : 4.00
Student Name: Trine Thunder
Student Id num: 1
Student GPA: 4.0


Using ==
Both are different
Using .equals() method
Both are different

Add a comment
Know the answer?
Add Answer to:
java This lab is intended to give you practice creating a class with a constructor method,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // 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 StudentC lient should contain the following executable code: package studentclient; public class...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

    PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT