copy constructors

i put the cpy ctr in the class, but how do i call it in the main method?

[79 byte] By [snappykr22a] at [2007-11-27 11:43:06]
# 1

no prlb dd jt cl t cp ctr on th nstns of ur clzz tht u crt 4 ur mn mthd

cotton.ma at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 2

public class X {

public int foo;

public X(int bar) {

foo = bar;

}

public X(X that) {

this.foo = that.foo;

}

public static void main(String[] args) {

X a = new X(17);

X b = new X(a); //<<--

}

}

BigDaddyLoveHandlesa at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 3

pbl st lk ths

pub sta vd mn(Str[] args)

{

MyOb x = nw MyOb();

MyOb y = nw MyOb(x);

}

Captain.Obviousa at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 4

BDLH FTW!

BigDaddyLoveHandlesa at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 5

2 mny ltrs..........................................

Captain.Obviousa at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 6

> BDLH FTW!

HAHAHAHAHAHAHA

TuringPesta at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 7

but how can i display to the user that if, lets say, object1 holds the same info as object2. in my main( ), i am trying to say that if object2 == object 1 to then display a message that says so.

snappykr22a at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 8

> BDLH FTW!

no bcuz i alrdy tld hm wht 2 do b4 u did

cotton.ma at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 9

> > BDLH FTW!

>

> no bcuz i alrdy tld hm wht 2 do b4 u did

T

BigDaddyLoveHandlesa at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 10

> but how can i display to the user that if, lets say,

> object1 holds the same info as object2. in my main(

> ), i am trying to say that if object2 == object 1 to

> then display a message that says so.

Please direct yourself here for answers to these questions http://java.sun.com/docs/books/tutorial/java/index.html

cotton.ma at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 11

> but how can i display to the user that if, lets say,

> object1 holds the same info as object2. in my main(

> ), i am trying to say that if object2 == object 1 to

> then display a message that says so.

Use equals, not ==. And did you override equals?

http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf

BigDaddyLoveHandlesa at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 12

here is my code....

public class StudentProg2

{

private String name; //student name

private int studentID; //student id number

private String course; //course abbrev

private String letterGrade; //student's final letter grade

private int examGrade1; //first exam grade

private int examGrade2; //second exam grade

private int examGrade3; //third exam grade

private int assignGrade1; //first assignment grade

private int assignGrade2; //second assignment grade

private int assignGrade3; //third assignment grade

private int totalGrade; // student's total number grade

/** The following constructors are created

to store the various information of the

Student class. */

public StudentProg2(String stuName, String courseAbrev, String ltrGrd)

{

name = stuName;

course = courseAbrev;

letterGrade = ltrGrd;

}

public StudentProg2() //default constructor set to return "Kristopher Russo"

{

name = "Krisotpher Russo";

}

public StudentProg2(int stuID, int exmGrd1, int exmGrd2,int exmGrd3,

int assignGrd1, int assignGrd2, int assignGrd3, int totalGrdNum)

{

studentID = stuID;

examGrade1 = exmGrd1;

examGrade2 = exmGrd2;

examGrade3 = exmGrd3;

assignGrade1 = assignGrd1;

assignGrade2 = assignGrd2;

assignGrade3 = assignGrd3;

totalGrade = totalGrdNum;

}

public String getName()

{

return name;

}

public String getCourseAbrev()

{

return course;

}

public String getLetterGrd()

{

return letterGrade;

}

public int getStuID()

{

return studentID;

}

public int getExmGrd1()

{

return examGrade1;

}

public int getExmGrd2()

{

return examGrade2;

}

public int getExmGrd3()

{

return examGrade3;

}

public int getAssignGrd1()

{

return assignGrade1;

}

public int getAssignGrd2()

{

return assignGrade2;

}

public int getAssignGrd3()

{

return assignGrade3;

}

public int getTotalGrd()

{

return examGrade1 + examGrade2 + examGrade3 + assignGrade1 +

assignGrade2 + assignGrade3; //adds the total of assignments and exams

}

public String toString()

{

//a string created to describe to user what program does

String info = "This program will display to the user information about the student.";

//return the string

return info;

}

/** following fields represent grades

on exams and assignments for student

*/

private int stu2exam1;

private int stu2exam2;

private int stu2exam3;

private int stu2assignment1;

private int stu2assignment2;

private int stu2assignment3;

//info about student2 to later check if he gets the same grade at student1

public StudentProg2(int stu2exm1, int stu2exm2, int stu2exm3,

int stu2assign1, int stu2assign2, int stu2assign3)

{

stu2exam1 = stu2exm1;

stu2exam2 = stu2exm2;

stu2exam3 = stu2exm3;

stu2assignment1 = stu2assign1;

stu2assignment2 = stu2assign2;

stu2assignment3 = stu2assign3;

}

public boolean equals(StudentProg2 Student2)

{

boolean studentCompare;

//determine whether this object's grades are equal to the other object's grades

if(examGrade1 == Student2.stu2exam1 &&

examGrade2 == Student2.stu2exam2 &&

examGrade3 == Student2.stu2exam3 && assignGrade1 == Student2.stu2assignment1 &&

assignGrade2 == Student2.stu2assignment2 &&

assignGrade3 == Student2.stu2assignment3)

studentCompare = true;

else

studentCompare = false;

//return the value

return studentCompare;

}

public StudentProg2(StudentProg2 copyObject)

{

this.studentID = copyObject.studentID;

this.examGrade1 = copyObject.examGrade1;

this.examGrade2 = copyObject.examGrade2;

this.examGrade3 = copyObject.examGrade3;

this.assignGrade1 = copyObject.assignGrade1;

this.assignGrade2 = copyObject.assignGrade2;

this.assignGrade3 = copyObject.assignGrade3;

this.totalGrade = copyObject.totalGrade;

}

}

public class StudentTestProg2

{

public static void main(String[] args)

{

StudentProg2 wcu = new StudentProg2("Kristopher Russo " , "CSC142" , "A");

StudentProg2 wcu2 = new StudentProg2(12345, 100, 100, 100, 10, 10, 10, 330);

StudentProg2 wcu3 = new StudentProg2(100, 100, 100, 10, 10, 10);

StudentProg2 CopyObject = new StudentProg2(wcu2); //copy constructor

System.out.println(wcu2); //executes the toString()

System.out.println("\nThe student's name is " + wcu.getName()

+ "(Student ID number is " + wcu2.getStuID()

+ "), "

+ "\nand the course he is taking is " + wcu.getCourseAbrev()

+ ".\nThe student's exam grades are as follows: "

+ wcu2.getExmGrd1() + ", " + wcu2.getExmGrd2() +", and "

+ wcu2.getExmGrd3() + ".\nThe student's assignment grades "

+ "are as follows: " + wcu2.getAssignGrd1() + ", "

+ wcu2.getAssignGrd2() + ", and " + wcu2.getAssignGrd3()

+ ".\nThe student's total grade is " + wcu2.getTotalGrd()

+ ".\nHis final letter grade base on all 3 exams (30% each)"

+ "\nand 3 program assignments (10%) is an " + wcu.getLetterGrd()

+ ".");

if(wcu2.equals(wcu3))

{

System.out.println("\nStudent1 and Student2 both received A's as their"

+ " final letter grade for the semester.");

}

else

{

System.out.println("\nStudent1 and Student2 did not receive the same "

+ "final letter grade.");

}

}

}

**********************************************************

what i am having trouble doing is on the printout, showing the user that these two students are actually the same student. any help? thanks.

snappykr22a at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 13

anyone?

snappykr22a at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...
# 14

> anyone?

Use [code] and [/code] tags around your code so it preserves proper formatting.

Tell us exactly what you want the program to output, and what it is outputting.

Tell us any exact error messages you are receiving.

Djaunla at 2007-7-29 17:48:01 > top of Java-index,Java Essentials,New To Java...