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

Answer the following JAVA programming questions except number 2 (the student cla

ID: 3811905 • Letter: A

Question

Answer the following JAVA programming questions except number 2 (the student class question).

Write the code to print each token. What is the return type of the method nextToken(). Suppose to want to peel off the next token for a double price. Write the line of code to do this: double value; Topic: writing and use of a copy constructor 1) What is the purpose of a copy constructor? 2) Write the code for a copy constructor for the Student class described above. 3) What is the difference between a deep copy and a shallow copy when writing a copy constructor? Topic: returning an object reference from a method 1) What is the return type from this method: public Fraction add(Fraction addend) 2) What is the parameter to this method: public Fraction add(Fraction addend) Topic: aggregation 1) What type of relationship is aggregation? 2) A class can only have primitive fields. True or false 3) Write a short class definition of a class using aggregation. Only declare the fields and write the constructor and the toString() methods.

Explanation / Answer

5) public String nextToken() // The return type of the method nextToken() is String
6) double value = Double.parseDouble(data);
   value =value*2;
   return new Double(value).toString();
  
  
Topic : copy constructor  
1) There are 2 good reasons for using a copy constructor instead of the constructor passing all parameters :

when you have a complex object with many attributes it is much more simpler to use the copy constructor
if you add an attribute to your class, you just change the copy constructor to take this new attribute into account instead of changing every occurence of the other constructor


3) Deep copy of an object will have exact copy of all the fields of original object just like shallow copy. But in additional, if original object has any references to other objects as fields, then copy of those objects are also created
  
   The shallow copy of an object will have exact copy of all the fields of original object. If original object has any references to other objects as fields, then only references of those objects are copied

Topic : returning ab object reference from a method  
1) return type of this method is the Object of Fraction class

2) the parameter of this method is the object of Fraction class.

Topic : Aggregation
1) Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.
   Example : Some 'common sense' examples of aggregations could perhaps include a team being an aggregate of its players, a village of its inhabitants, a company of its workforce or a society and its membership.
  
2) False

3) // example

   class Author
{
String authorName;
int age;
String place;
Author(String name,int age,String place)
{
this.authorName=name;
this.age=age;
this.place=place;
}
public String getAuthorName()
{
return authorName;
}
public int getAge()
{
return age;
}
public String getPlace()
{
return place;
}
}

class Book
{
String name;
int price;
Author auth;
Book(String n,int p,Author at)
{
this.name=n;
this.price=p;
this.auth=at;
}
public void toString()
{
System.out.println("Book is:"+name +" " +"price:"+price +" " +"Author is:"+auth.getAuthorName());
  
}
}

class Test
{
public static void main(String args[])
{
Author ath=new Author("Me",22,"India");
Book b=new Book("Java",550,ath);
b.toString();
}
}