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

Consider the following java class: public class Grapes { private String name; pr

ID: 668105 • Letter: C

Question

Consider the following java class:

public class Grapes {

private String name;

private String type;

private double value;

private int number;

public Grape (String newName, String newType, double value, int number){

name = newName;

type = newType;

value = newValue;

number = newNumber;

}

public String toString() {

String s = name + " " + type + " " + value + " " + number;

return s;

}

}

What type of variable is name?

What type of variable is value?

What type of variable is s?

Assume s1 has been instantiated already. Write a statement to print a text representation of s1 to the console.

Explanation / Answer

Type of variable : name

The name is string type variable and it is the private member variable of Grapes class

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Type of variable : value

The value is double type of variable and it is the private member variable of Grapes class

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Type of variable :s

The s is a string type variable and it is a local variable.
This variable , s is not accessible to the outside of the method.

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Statement to print a text presentation of s1

//Create an instance of the Grapes class
Grapes grapeObject=new Grapes("Alaska", "Fruit", 50.0,10);
//print grapeObject on console by calling toString method of Grapes class
//object
System.out.println(grapeObject.toString());

----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Grapes class definiton with private members and public
//methods
//Grapes.java
public class Grapes
{
  
   //name is string type variable and it is the private member variable of Grapes class
   private String name;  
   private String type;
   //value is double type of variable and it is the private member variable of Grapes class
   private double value;  
   private int number;
  
  
   //Constructor of the class must have same name as class name  

    //parameters in and out in the constructor while initialization must be equal
   public Grapes(String newName, String newType, double newValue, int newNumber)
   {
       name = newName;
       type = newType;
       value = newValue;
       number = newNumber;
   }

   public String toString()
   {
       //s1 is a string type variable and it is a local variable.
       //This variable , s is not accessible to the outside of the method
       String s1 = name + " " + type + " " + value + " " + number;
       //returns s value as string representation
       return s1;
   }
}

--------------------------------------------------------------------------------------------------------------------------------------------

//Tester.java
public class Tester
{
   public static void main(String[] args)
   {
       //Create an instance of the Grapes class
       Grapes grapeObject=new Grapes("Alaska", "Fruit", 50.0,10);
       //print grapeObject on console by calling toString method of Grapes class
       //object

       System.out.println(grapeObject.toString());
      
   }
}


--------------------------------------------------------------------------------------------------------------------------------------------------------

Compile : javac Tester.java

Run :java Tester

sample output:

Alaska   Fruit   50.0   10

Note : In the program no s1 is there. For convience, s changed to s1 in toString method to print as text represenation.

Hope this helps you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote