Question

In java Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following...

In java

Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following methods:

A ‘copy’ constructor,

A ‘clone’ method,

A ‘finalize’ method,

A ‘dispose’ method, and

A ‘hashCode’ method.

A ‘compareTo’ method

Test the upgraded classes using the application ‘TestStudentName.java’ located in ‘Chapter01_Files.zip’. Be sure to include output messages in both the ‘finalize’ method and the ‘dispose’ method.

Chapter01 File 'TestStudentName.java' below.

// -----------------------------------------------

// TestStudentName.java

// -----------------------------------------------

public class TestStudentName

{

   public static void main ( String [ ] args )

   {

       Name n1 = new Name ( "Ty", "Cobb" );

       Name n2 = new Name ( "Babe", "Ruth" );

   // ---- Test the copy constructor --------

       System.out.println ( "Test the copy constructor ------------" );

       Student s1 = new Student ( n1, "123456" );

       Student s2 = new Student ( s1 );

       s2.setStudent ( n2, "234567" );

       if ( s1.equals ( s2 ) )

       {

           System.out.println ( "\t\tError - students should not be the same" );

           System.out.println ( "\t\ts1 = " + s1 );

           System.out.println ( "\t\ts1 = " + s2 );

       }

       else

       {

           System.out.println ( "\t\tSuccess - students are not the same" );

           System.out.println ( "\t\ts1 = " + s1 );

           System.out.println ( "\t\ts1 = " + s2 );

       }

   // ---- Test the clone method ------------

       System.out.println ( "\n\nTest the 'clone' method ------------" );

       Student s3 = (Student) s1.clone ( );

       if ( s1.equals ( s3 ) )

           System.out.println ( "\t\tSuccess - Students s1 and s3 are the same." );

       else

       {

           System.out.println ( "\t\tError - Students s1 and s3 are not the same" );

           System.out.println ( "\t\ts1 = " + s1 );

           System.out.println ( "\t\ts3 = " + s3 );

       }

       s3.setStudent ( n2, "234567" );

       if ( s1.equals ( s3 ) )

       {

           System.out.println ( "\t\tError - students should not be the same" );

           System.out.println ( "\t\ts1 = " + s1 );

           System.out.println ( "\t\ts1 = " + s3 );

       }

       else

           System.out.println ( "\t\tSuccess - students are not the same" );

   // ---- Test the finalize method ---------

       System.out.println ( "\n\nTest the 'finalize' method ------------" );

       s1 = null;

       System.gc();

       System.out.println ( "\t\tShould see the 'finalize' message ------------" );

   // ---- Test the dispose method ----------

       System.out.println ( "\n\nTest the 'dispose' method ------------" );

       s2.dispose();

       System.out.println ( "\t\tShould see the 'dispose' message ------------" );

       s2 = null;

   // ---- Test the hashCode method ---------

       s1 = new Student ( s3 );

       System.out.println ( "\n\nTest the 'hashCode' method ------------" );

       if ( s1.hashCode ( ) == s3.hashCode ( ) )

           System.out.println ( "\t\tSuccess - hashCode for s1 and s3 are the same." );

       else

       {

           System.out.println ( "\t\tError - hashCode for s1 and s3 are not the same." );

           System.out.println ( "\t\ts1.hashCode = " + s1.hashCode() );

           System.out.println ( "\t\ts3.hashCode = " + s3.hashCode() );

       }

       System.out.println ( );

   }

}

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

TestingStudentName.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package testingstudentname;

public class TestingStudentName

{

public static void main ( String [ ] args ) throws CloneNotSupportedException

{

Name n1 = new Name ( "Ty", "Cobb" );

Name n2 = new Name ( "Babe", "Ruth" );

// ---- Test the copy constructor --------

System.out.println ( "Test the copy constructor ------------" );

Student s1 = new Student ( n1, "123456" );

Student s2 = new Student ( s1 );

s2.setStudent ( n2, "234567" );

if ( s1.equals ( s2 ) )

{

System.out.println ( "\t\tError - students should not be the same" );

System.out.println ( "\t\ts1 = " + s1 );

System.out.println ( "\t\ts1 = " + s2 );

}

else

{

System.out.println ( "\t\tSuccess - students are not the same" );

System.out.println ( "\t\ts1 = " + s1 );

System.out.println ( "\t\ts1 = " + s2 );

}

// ---- Test the clone method ------------

System.out.println ( "\n\nTest the 'clone' method ------------" );

Student s3 = (Student) s1.clone();

if ( s1.equals ( s3 ) )

System.out.println ( "\t\tSuccess - Students s1 and s3 are the same." );

else

{

System.out.println ( "\t\tError - Students s1 and s3 are not the same" );

System.out.println ( "\t\ts1 = " + s1 );

System.out.println ( "\t\ts3 = " + s3 );

}

s3.setStudent ( n2, "234567" );

if ( s1.equals ( s3 ) )

{

System.out.println ( "\t\tError - students should not be the same" );

System.out.println ( "\t\ts1 = " + s1 );

System.out.println ( "\t\ts1 = " + s3 );

}

else

System.out.println ( "\t\tSuccess - students are not the same" );

// ---- Test the finalize method ---------

System.out.println ( "\n\nTest the 'finalize' method ------------" );

s1 = null;

System.gc();

System.out.println ( "\t\tShould see the 'finalize' message ------------" );

// ---- Test the dispose method ----------

System.out.println ( "\n\nTest the 'dispose' method ------------" );

s2.dispose();

System.out.println ( "\t\tShould see the 'dispose' message ------------" );

s2 = null;

// ---- Test the hashCode method ---------

s1 = new Student ( s3 );

System.out.println ( "\n\nTest the 'hashCode' method ------------" );

if ( s1.hashCode ( ) == s3.hashCode ( ) )

System.out.println ( "\t\tSuccess - hashCode for s1 and s3 are the same." );

else

{

System.out.println ( "\t\tError - hashCode for s1 and s3 are not the same." );

System.out.println ( "\t\ts1.hashCode = " + s1.hashCode() );

System.out.println ( "\t\ts3.hashCode = " + s3.hashCode() );

}

System.out.println ( );

}

}

Name.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package testingstudentname;

/**

class Name {

private String ty;

private String cobb;

Name(String ty, String cobb) {

this.ty =ty;

this.cobb =cobb;

}

  

}

Student.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package testingstudentname;

/**

class Student implements Cloneable {

private Name n1;

private String string;

private Name n2;

Student(Student s3) {

}

Student(Name n1, String string) {

this.n1 = n1;

this.string = string;

}

void setStudent(Name n2, String string) {

this.n2 =n2;

this.string=string;

}

void dispose() {

}

public Object clone() throws CloneNotSupportedException

{

return super.clone();

}

}

Output:

Projects ×郾TestingStudentName ja a ×眧output TestingStudentName run XE Name Java × Student ava x run: Test the copy constructo

Add a comment
Know the answer?
Add Answer to:
In java Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following...
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
  • JAVA 1.            Given the following class definition, what are the contents of the fields x and...

    JAVA 1.            Given the following class definition, what are the contents of the fields x and y of the object p ?    class MyPoint {      int x;      int y; public MyPoint (int x, int y){        x = x;        y = y;      } --------------------------------------- MyPoint p = new MyPoint( 100, 88 ); 2.            static and non-static members What would happen if you tried to compile and run the following code ? public class Driver {...

  • 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)...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    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...

  • JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** *...

    JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** * Write a description of class UniversityTester here. * * @author (your name) * @version (a version number or a date) */ public class ClassroomTester { public static void main(String[] args) { ArrayList<Double> grades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList<Double> grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList<Double> grades3 = new...

  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • For Python 3.7+. TEST THE CODE WITH THE FOLLOWING GLOBAL CODE AFTER YOU'RE DONE: print('\nStart of...

    For Python 3.7+. TEST THE CODE WITH THE FOLLOWING GLOBAL CODE AFTER YOU'RE DONE: print('\nStart of A2 Student class demo ') s1 = Student('David Miller', major = 'Hist',enrolled = 'y', credits = 0, qpoints = 0) s2 = Student('Sonia Fillmore', major = 'Math',enrolled = 'y', credits = 90, qpoints = 315) s3 = Student('A. Einstein', major = 'Phys',enrolled = 'y', credits = 0, qpoints = 0)          s4 = Student('W. A. Mozart', major = 'Mus',enrolled = 'n', credits = 29, qpoints...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

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