Create class MobilePhone with member variables
- ModelNumber
- ScreenSize
- Memory
- Color
- Price
Write a test class with below methods.
Create another method called updateMobilePhonePrice(MobilePhone mobilePhone) and it should return MobilePhone reference.
Inside this method, change the price using mobilePhone reference variable.
In the next line, print the price using mobilePhone reference variable.
After that create new MobilePhone object using parameterized constructor with different price and assign it to same mobilePhone reference variable.
mobilePhone = new MobilePhone;
Then return mobilePhone reference.
Now in main method,
call updateMobilePhonePrice by passing mobilePhone1 and assign the returned value back to mobilePhone1.
mobilePhone1 = updateMobilePhonePrice(mobilePhone1);
Next print the price using mobilePhone1.
Code :-
public class MobilePhone {
int ModelNumber;
int ScreenSize;
int Memory;
String Color;
int Price;
MobilePhone(int modelNumber, int screenSize, int memory, String color, int price) {
this.ModelNumber = modelNumber;
this.ScreenSize = screenSize;
this.Memory = memory;
this.Color = color;
this.Price = price;
}
public MobilePhone updateMobilePhonePrice(MobilePhone mobilePhone) {
this.ScreenSize = mobilePhone.ScreenSize;
System.out.println(mobilePhone.Price);
mobilePhone = new MobilePhone(123,6,12,"White",35000);
return mobilePhone;
}
public static void main(String[] args) {
MobilePhone mobilePhone1 = new MobilePhone(341,5,8,"Black", 25000);
mobilePhone1 = mobilePhone1.updateMobilePhonePrice(mobilePhone1);
System.out.println(mobilePhone1.Price);
}
}