In: Computer Science
Hello,
I’m having some trouble making changes to the Cellphone and the associated tester class;
Any help is appreciated !
-Implement both get and set methods for all 3 attributes or fields of the class
- Fields/Attributes are all initialized using the setter methods and there’s no need to initialize them using the contractor.
2) Tester Class;
-Create the objects using the default constructor. This is the constructor with no parameter
-Call the set methods of all 3 fields/attributes to set their initial values.
-No changes to the other functionalities of the tester class.
IDE; drjava
CellPhone.java
-------------------------------------------------------------------------------
/*
* This is CellPhone class with variables
* Manufacturer Name, Retail Price and Model name.
* It has its getter and setter methods along with the
* methods to calculate the increment and decrement of price.
*/
public class CellPhone {
private String manufact_name;
private double retail_price;
private String model;
public CellPhone(String name, double price, String
model) {
this.manufact_name = name;
this.retail_price = price;
this.model =
model;
}
/**
* @return the manufact_name
*/
public String getManufact_name() {
return manufact_name;
}
/**
* @return the retail_price
*/
public double getRetail_price() {
return retail_price;
}
/**
* @param retail_price the retail_price to set
*/
public void setRetail_price(double retail_price)
{
this.retail_price =
retail_price;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
public double increase_price(double inc) {
retail_price = retail_price +
inc;
return
retail_price;
}
public double decrease_price(double dec) {
retail_price = retail_price -
dec;
return retail_price;
}
}
CPTester.java
-------------------------------------------------------------------------------
/*
* This class tests the CellPhone.java class. It creates two objects
for CellPhone class
* Gets the input from User through Scanner class. Increase/Decrease
the price of CellPhone
* and display the CellPhone object information.
*/
import java.util.Scanner;
public class CPTester {
public static void main(String[]args) {
Scanner in = new
Scanner(System.in);
System.out.print("Please enter
the CellPhone1 Manufacturer Name:");
String manufact_name1 =
in.next();
System.out.print("Please enter the
CellPhone1 Retail Price :");
double price1 =
in.nextDouble();
System.out.print("Please enter the
CellPhone1 Model Name :");
String model1 = in.next();
CellPhone cp1 = new
CellPhone(manufact_name1,price1, model1);
System.out.println("CellPhone1
Manufacturer Name :"+cp1.getManufact_name());
System.out.println("CellPhone1
Retail price :"+cp1.getRetail_price());
System.out.println("CellPhone1
Model :"+cp1.getModel());
//Increased price
System.out.print("Please enter the
CellPhone1 price increment :");
double inc=in.nextDouble();
//Price after increase
System.out.println("CellPhone1
Retail price After increase :"+cp1.increase_price(inc));
System.out.print("Please enter the
CellPhone2 Manufacturer Name:");
String manufact_name2 =
in.next();
System.out.print("Please enter the
CellPhone2 Retail Price :");
double price2 =
in.nextDouble();
System.out.print("Please enter the
CellPhone2 Model Name :");
String model2 = in.next();
CellPhone cp2 = new
CellPhone(manufact_name2,price2, model2);
System.out.println("CellPhone2
Manufacturer Name :"+cp2.getManufact_name());
System.out.println("CellPhone2
Retail price :"+cp2.getRetail_price());
System.out.println("CellPhone2
Model :"+cp2.getModel());
//Decreased price
System.out.print("Please enter the
CellPhone2 price Decrease :");
double dec=in.nextDouble();
//Price after decrease
System.out.println("CellPhone2
Retail price After decrease
:"+cp2.decrease_price(dec));
}
}
Hey! here my answer is given below...to appreciate my work please give a positive rating......
CellPhone.java setManufact_name() set function is added .......
/*
* This is CellPhone class with variables
* Manufacturer Name, Retail Price and Model name.
* It has its getter and setter methods along with the
* methods to calculate the increment and decrement of price.
*/
public class CellPhone {
private String manufact_name;
private double retail_price;
private String model;
/**
* @return the manufact_name
*/
public String getManufact_name() {
return manufact_name;
}
/**
* @param manufact_name the manufact_name to set
*/
public void setManufact_name(String manufact_name) {
this.manufact_name = manufact_name;
}
/**
* @return the retail_price
*/
public double getRetail_price() {
return retail_price;
}
/**
* @param retail_price the retail_price to set
*/
public void setRetail_price(double retail_price) {
this.retail_price = retail_price;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
public double increase_price(double inc) {
retail_price = retail_price + inc;
return retail_price;
}
public double decrease_price(double dec) {
retail_price = retail_price - dec;
return retail_price;
}
}
CPTester.java , updated code with explanation.....
/*
* This class tests the CellPhone.java class. It creates two objects for CellPhone class
* Gets the input from User through Scanner class. Increase/Decrease the price of CellPhone
* and display the CellPhone object information.
*/
import java.util.Scanner;
public class CPTester {
public static void main(String[]args) {
Scanner in = new Scanner(System.in);
/* take input from user for object cp1*/
System.out.print("Please enter the CellPhone1 Manufacturer Name:");
String manufact_name1 = in.next();
System.out.print("Please enter the CellPhone1 Retail Price :");
double price1 = in.nextDouble();
System.out.print("Please enter the CellPhone1 Model Name :");
String model1 = in.next();
/*create cp1 object of CellPhone class
call setter method of 3 variables to set the value of user*/
CellPhone cp1 = new CellPhone();
cp1.setManufact_name(manufact_name1);
cp1.setRetail_price(price1);
cp1.setModel(model1);
/* call getter method of 3 variables to extract the stored data of cp1 */
System.out.println("CellPhone1 Manufacturer Name :"+cp1.getManufact_name());
System.out.println("CellPhone1 Retail price :"+cp1.getRetail_price());
System.out.println("CellPhone1 Model :"+cp1.getModel());
//Increased price
System.out.print("Please enter the CellPhone1 price increment :");
double inc=in.nextDouble();
//Price after increase
System.out.println("CellPhone1 Retail price After increase :"+cp1.increase_price(inc));
/* take input from user for object cp2*/
System.out.print("Please enter the CellPhone2 Manufacturer Name:");
String manufact_name2 = in.next();
System.out.print("Please enter the CellPhone2 Retail Price :");
double price2 = in.nextDouble();
System.out.print("Please enter the CellPhone2 Model Name :");
String model2 = in.next();
/*create cp2 object of CellPhone class
call setter method of 3 variables to set the value of user*/
CellPhone cp2 = new CellPhone();
cp2.setManufact_name(manufact_name2);
cp2.setModel(model2);
cp2.setRetail_price(price2);
/* call getter method of 3 variables to extract the stored data of cp1 */
System.out.println("CellPhone2 Manufacturer Name :"+cp2.getManufact_name());
System.out.println("CellPhone2 Retail price :"+cp2.getRetail_price());
System.out.println("CellPhone2 Model :"+cp2.getModel());
//Decreased price
System.out.print("Please enter the CellPhone2 price Decrease :");
double dec=in.nextDouble();
//Price after decrease
System.out.println("CellPhone2 Retail price After decrease :"+cp2.decrease_price(dec));
}
}
this updated code works fine and fulfills all the requirements...........
thanks for any query please comment in the comment section.......