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

Create a new Java project called inlab4. 2. Create a package called model and a

ID: 3715206 • Letter: C

Question

Create a new Java project called inlab4. 2. Create a package called model and a package called test. 3. In the model package, add the following class: package model; public class Laptop { private String maker; private double price; private static int numberOfLaptops; public Laptop(String maker, double price) { this.maker = maker; this.price = price; numberOfLaptops++; } public String getMaker() { return maker; } public double getPrice() { return price; } public void setMaker(String maker) { this.maker = maker; } public void setPrice(double price) { if (price<0) this.price = price; else this.price = 0; } public String toString(){ return String.format("Marker: %s, Price: QR%.2f", maker,price); } } 4. In the model, add a class called Person that contains three data fields: id (int), name (String) and laptop (Laptop). Your class should have a constructor that initializes the three instance variables. Provide the setter and the getter method for each instance variable. a) Add a method called hasLaptop() that returns true if the laptop instance variable is not null and returns false otherwise.

b) Add a toString() method that returns the person name and all the information about his/her laptop but only if the student has a laptop (hint: use hasLaptop()). If the person doesn’t have a laptop then the method returns only the name of the student.


5. In the test package of your project create another Java class called personTest that includes a main()method in which you do the following (in the given sequence):



  

Explanation / Answer

person class:

package model;

//person class

public class Person

{

//data fields:

int id;

String name;

Laptop laptop;

//constructor that initializes the three instance variables.

public Person(int id, String name, Laptop laptop)

{

super();

this.id = id;

this.name = name;

this.laptop = laptop;

}

//setter and the getter method for each instance variable.  

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Laptop getLaptop() {

return laptop;

}

public void setLaptop(Laptop laptop) {

this.laptop = laptop;

}

//hasLaptop() method

public boolean hasLaptop()

{

//false if the laptop instance variable is null

if(laptop==null)

return false;

else //true if the laptop instance variable is not null

return true;

}

//toString()

public String toString()

{

if(this.hasLaptop())

return "Person [id=" + id + ", name=" + name + ", laptop=" + laptop + "]";

else

return "Person [name=" + name+"]";

}

}

personTest class:

package test;

import model.Laptop;

import model.Person;

public class personTest

{

public static void main(String[] args)

{

//Creating object of Laptop class

Laptop laptop= new Laptop("Dell",34500);

System.out.println(laptop.toString());

//Creating object of Person class

Person person = new Person(1234,"Ruther",laptop);

//calling hasLaptop() method

boolean hasLaptop= person.hasLaptop();

//printing result

System.out.println("HasLaptop?"+hasLaptop);

//calling toString() method

System.out.println(person.toString());

//make laptop null and check the methods

laptop=null;

//creating another person object

Person person2 = new Person(1234,"Ruther",laptop);

hasLaptop= person2.hasLaptop();

System.out.println("HasLaptop?"+hasLaptop);

System.out.println(person2.toString());

}

}

Output:

Marker: Dell, Price: QR34500.00
HasLaptop?true
Person [id=1234, name=Ruther, laptop=Marker: Dell, Price: QR34500.00]
HasLaptop?false
Person [name=Ruther]

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