In Java 1. Create a class named after your favorite object in the whole world. F
ID: 3594148 • Letter: I
Question
In Java
1. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method
2. Create Instance Variables (attributes) (1 point)
Create at least 3 private instance fields (attributes) for your class
You must use at least 3 different data types for your fields
3. Create getter (accessor) and setter (mutator) methods
Create a getter (accessor) method for each of your instance variables (1 point)
Create a setter (mutator) method for each of your instance variables (1 point)
4. Create a Method to display your data (1 point)
Create a method called display, that simply prints out the values of all instance variables of your object
5. Create 2 Constructors
Create a default constructor (no parameters) that assigns all your instance variables to default values (1 point)
Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters (1 point)
6. Testing your program (1 point)
Create a class called Demo.java. This class will contain your main method
Create an instance of your class by using the default constructor.
Call all your objects set methods to assign values to your object
Call the objects display method, to print out it's values
Create another instance of your class by using the parameterized constructor
Call the objects display method, to print out it's values
Explanation / Answer
Mobile.java
public class Mobile {
//Declaring instance variables
private String make;
private String model;
private double price;
//Zero argumented constructor
public Mobile() {
}
//Parameterized constructor
public Mobile(String make, String model, double price) {
super();
this.make = make;
this.model = model;
this.price = price;
}
//getters and setters
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//This method is used to display the contents of an object inside it
public void display() {
System.out.println("Mobile Make :" + make);
System.out.println("Mobile Model :" + model);
System.out.println("Price :$" + price);
}
}
__________________
Demo.java
public class Demo {
public static void main(String[] args) {
//Creating an Mobile class using default constructor
Mobile m = new Mobile();
//Calling the setter methods
m.setMake("Apple");
m.setModel("Iphone 7s");
m.setPrice(70000.00);
//calling the display method on the Mobile class
m.display();
//Creating an Mobile class using default constructor
Mobile m1 = new Mobile("Samsung", "Galaxy 7s", 65000);
//calling the display method on the Mobile class
m1.display();
}
}
_________________
Output:
Mobile Make :Apple
Mobile Model :Iphone 7s
Price :$70000.0
Mobile Make :Samsung
Mobile Model :Galaxy 7s
Price :$65000.0
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.