In this assignment you are required to write and use some user-defined classes.
ID: 3680418 • Letter: I
Question
In this assignment you are required to write and use some user-defined classes. Here is what is given. A cell phone object has three attributes, a brand (String), a serial number (long), and a price (double).
Part 1:
You are required to design and implement the CellPhone class according to the following specifications:
- Upon the creation of a cell phone object, the object must immediately be initialized with valid values; that is brand, serial number and price.
- The design should allow enough flexibility so that the value of any of these attributes can be modified later on. For example, it should be possible to create a cell phone object with a given price then change its price later on.
- The design should allow the user of the class to obtain the value of any of the attributes individually. The design should also allow the user to view all cell phone information at once by passing cell phone objects to the print/println() methods;
- The design should allow for one cell phone to be compared to another cell phone for equality. Two cell phone objects are considered equal if they have the same brand and the same price.
Part 2:
In that part, you are required to write a public class called UtilizeCellPhones, which is going to utilize the CellPhone class that you have created in part1. In the main method of this class, you should perform the following operations:
1) Create three cell phone objects; notice that you must assign a brand, serial number (you can initialize it as any 9-digit number) and a price to each of these objects upon creation.
2) Show all information of each of the three objects.
3) Change the price of the first object; the price and the brand of the second object, and the serial number value of the third object.
4) Display only the modified attributes of the three objects. All outputs showing only the price of a cell phone (i.e. not the rest of the attributes) must be formatted.
5) Compare some of these cell phone objects for equality and display whether or not they are equal.
Explanation / Answer
package myproject;
// import packages
import java.util.Scanner;
/**
*
* @author Shivakumar
*/
public class UtilizeCellPhones {
int s;
public static void main(String[] args) // main starts here
{
String brandref; // brandref string to identify obj
Scanner sc=new Scanner(System.in); // sccaner obj for console input
CellPhone c1=new CellPhone("Apple",123456789,50000); // creation of cellphone objects
CellPhone c2=new CellPhone("samsung",987654321,45000);
CellPhone c3=new CellPhone("lenovo",957388321,35000);
c1.showinfo(); // printing the details
c2.showinfo();
c3.showinfo();
System.out.println("Do u want to modify? Y?N"); // modification
brandref=sc.next();
if(brandref.equals("Y")) // condition weather modify or not
{
System.out.println("Enter brand name for which u want to modify:");
brandref=sc.next();
switch (brandref) { // switch for finding object
case "apple":
c1.modify();
break;
case "samsung":
c2.modify();
break;
case "lenovo":
c3.modify();
break;
default:
System.out.println("Enter brand name correctly");
break;
}
}
System.out.println("After modification:"); // printing after modification
c1.showinfo();
c2.showinfo();
c3.showinfo();
System.out.println("C1 and c2:"+c1.equals(c2));
System.out.println("C1 and c3:"+c1.equals(c3)); // comparing objects
System.out.println("C2 and c3:"+c3.equals(c2));
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package myproject;
import java.util.Scanner;
/**
*
* @author Shivakumar
*/
public class CellPhone {
String brand; long serial; double price;
public CellPhone(String brand,long serial,double price) //method for initializing values
{
this.brand=brand;
this.price=price;
this.serial=serial;
}
public void showinfo() // method to show the values
{
System.out.println("Cell info brand: "+brand+" serial:"+serial+" price:"+price);
}
public void modify() // method to modify the values
{ String m;
Scanner scm=new Scanner(System.in);
System.out.println("Do you want to modify brand? Y/N");
m=scm.next();
if(m.equals("Y")) // asking user for modification
{
System.out.println("Enter new brand name:");
brand=scm.next();
}
System.out.println("Do you want to modify serial? Y/N");
m=scm.next();
if(m.equals("Y"))
{
System.out.println("Enter new serial no:");
serial=scm.nextLong();
}
System.out.println("Do you want to modify price? Y/N");
m=scm.next();
if(m.equals("Y"))
{
System.out.println("Enter new price:");
price=scm.nextDouble();
}
}
}