You are on page 1of 5

CPSC6548 Lab Assignment #4

Assigned: Week 4 Due: Week 5 Points: 40

Write a Java application, and an additional class to represent some real-world entity such as a
technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in
code of something real or imagined, which has attributes (member variables) and behaviors
(member methods).

Note: I will go through and post examples of a SmartPhone class and a FootballPlayer class in
class, so choose something other than these.

The class will:


a. Create a total of 5 member variables for the class, selecting the appropriate data types for each
field. For example, a class to represent a lamp might include color, price, height, numBulbs,
batteryOperated. Each of these 5 variables need a data type.
b. Include at least three different constructor methods, in addition to the default constructor (0
argument constructor). The constructor is a function (method) which allocates memory and
initialized the member variables specified in (a.).
c. Include getters/setters for to serve as as mutators and accessors for each variable. Name these
appropriately such as setColor & getColor, setPrice & getPrice, setHeight & getHeight,
setNumBulbs and getNumBulbs, and setBatteryOperated & getBatteryOperated.
d. Create a member function showValues() to display the values of an object in a formatted
manner.
e. Create at least 2 other member functions (methods) for the class that will perform some
operation on the data (i.e. calculations or additional report/display of output). For example,
turnLampOn, changeBulb, etc.

The Java application class (with a main method) will:


a. Instantiate at least three objects (using each of the constructors at least once) with your
program.
Note: Set the remaining data with the mutator methods.
b. Store the data from the individual objects into an ArrayList (or some other dynamic data
structure of objects.
c. Utilize the showValues() method and each of the two methods on each of your objects to
demonstrate their use.

Required Output: Generate output samples demonstrating all of your member functions,
adequately testing them and showing their functionality (i.e. inputting values, outputting
values (displaying them), performing calculations, etc.).
Laptop.java

public class Laptop {


private String make,OperatingSystem;
private int numberOfProcess,ram;
private boolean powerOn;

public Laptop(String make, String operatingSystem, int numberOfProcess, int ram,


boolean powerOn) {
this.make = make;
OperatingSystem = operatingSystem;
this.numberOfProcess = numberOfProcess;
this.ram = ram;
this.powerOn = powerOn;
}

public Laptop(String make, String operatingSystem, int ram) {


this.make = make;
OperatingSystem = operatingSystem;
this.numberOfProcess = 0;
this.ram = ram;
this.powerOn = false;
}

public Laptop(String make, String operatingSystem, int ram, boolean powerOn) {


this.make = make;
OperatingSystem = operatingSystem;
this.numberOfProcess = 0;
this.ram = ram;
this.powerOn = powerOn;
}

public String getMake() {


return make;
}

public void setMake(String make) {


this.make = make;
}

public String getOperatingSystem() {


return OperatingSystem;
}

public void setOperatingSystem(String operatingSystem) {


OperatingSystem = operatingSystem;
}

public int getNumberOfProcess() {


return numberOfProcess;
}

public void setNumberOfProcess(int numberOfProcess) {


this.numberOfProcess = numberOfProcess;
}

public int getRam() {


return ram;
}
public void setRam(int ram) {
this.ram = ram;
}

public boolean isPowerOn() {


return powerOn;
}

public void setPowerOn(boolean powerOn) {


this.powerOn = powerOn;
}

public void showValues() {


System.out.println("make: " + make +
"\nOperatingSystem: " + OperatingSystem +
"\nnumberOfProcess: " + numberOfProcess +
"\nram: " + ram +
"\npowerOn: " + powerOn);
}
public void turnOn(){
this.powerOn=true;
}
public void turnOff(){
this.powerOn=false;
}
public void createProcess(){
++this.numberOfProcess;
}
public void terminateProcess(){
--this.numberOfProcess;
}
}

main.java
public class Main {

public static void main(String[] args) {


List<Laptop> laptops=new ArrayList<>();

System.out.println("\nLaptop 1 details\n-----------------------");
Laptop laptop1=new Laptop("HP","Windows",0,4,true);
laptop1.showValues();
laptops.add(laptop1);

System.out.println("\nLaptop 2 details\n-----------------------");
Laptop laptop2=new Laptop("Acer","Linux",8,true);
laptop2.setNumberOfProcess(10);
laptop2.showValues();
laptops.add(laptop2);

System.out.println("\nLaptop 3 details\n-----------------------");
Laptop laptop3=new Laptop("Lenovo","Ubuntu",16);
laptop3.setPowerOn(false);
laptop3.setNumberOfProcess(0);
laptop3.showValues();
laptops.add(laptop3);

System.out.println("\nMethods\n");
System.out.println("\nturning laptop3 on\n-----------------------");
laptop3.turnOn();
laptop3.showValues();

System.out.println("\ncreating a process in laptop3\n---------------------");


laptop3.createProcess();
laptop3.showValues();

System.out.println("\ncreating a process in laptop3\n---------------------");


laptop3.createProcess();
laptop3.showValues();

System.out.println("\nterminate a process in laptop3\n--------------------");


laptop3.terminateProcess();
laptop3.showValues();

System.out.println("\nturning laptop3 off\n-----------------------");


laptop3.turnOff();
laptop3.showValues();

}
}

Output

Laptop 1 details
-----------------------
make: HP
OperatingSystem: Windows
numberOfProcess: 0
ram: 4
powerOn: true

Laptop 2 details
-----------------------
make: Acer
OperatingSystem: Linux
numberOfProcess: 10
ram: 8
powerOn: true

Laptop 3 details
-----------------------
make: Lenovo
OperatingSystem: Ubuntu
numberOfProcess: 0
ram: 16
powerOn: false

Methods

turning laptop3 on
-----------------------
make: Lenovo
OperatingSystem: Ubuntu
numberOfProcess: 0
ram: 16
powerOn: true

creating a process in laptop3


-----------------------
make: Lenovo
OperatingSystem: Ubuntu
numberOfProcess: 1
ram: 16
powerOn: true

creating a process in laptop3


-----------------------
make: Lenovo
OperatingSystem: Ubuntu
numberOfProcess: 2
ram: 16
powerOn: true

terminate a process in laptop3


-----------------------
make: Lenovo
OperatingSystem: Ubuntu
numberOfProcess: 1
ram: 16
powerOn: true

turning laptop3 off


-----------------------
make: Lenovo
OperatingSystem: Ubuntu
numberOfProcess: 1
ram: 16
powerOn: false

Process finished with exit code 0

You might also like