0% found this document useful (0 votes)
51 views

04 Laboratory Exercise 1

This document defines an abstract Laptop class with methods to return RAM, SSD, and CPU specifications. Concrete Minimum and Recommended subclasses implement these methods by storing property values passed to their constructors. A LaptopFactory class returns instances of the subclasses based on a type string, allowing a main method to output laptop specifications.

Uploaded by

Gosen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

04 Laboratory Exercise 1

This document defines an abstract Laptop class with methods to return RAM, SSD, and CPU specifications. Concrete Minimum and Recommended subclasses implement these methods by storing property values passed to their constructors. A LaptopFactory class returns instances of the subclasses based on a type string, allowing a main method to output laptop specifications.

Uploaded by

Gosen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Abstract Class:

public abstract class Laptop


{
public abstract int getRAM();
public abstract int getSSD();
public abstract String getCPU();

@Override
public String toString()
{
return "RAM=" + this.getRAM() + "GB, SSD=" + this.getSSD() + "CPU=" +
this.getCPU();
}
}

Sub Class:
public class Minimum extends Laptop
{
private final int ram;
private final int ssd;
private final String cpu;

public Minimum(int ram, int ssd, String cpu)


{
this.ram = ram;
this.ssd = ssd;
this.cpu = cpu;
}
@Override
public int getRAM()
{
return ram;
}
@Override
public int getSSD()
{
return ssd;
}
@Override
public String getCPU()
{
return cpu;
}
}

Sub Class:
public class Recommended extends Laptop
{
private final int ram;
private final int ssd;
private final String cpu;

public Recommended(int ram, int ssd, String cpu)


{
this.ram = ram;
this.ssd = ssd;
this.cpu = cpu;
}
@Override
public int getRAM()
{
return ram;
}
@Override
public int getSSD()
{
return ssd;
}
@Override
public String getCPU()
{
return cpu;
}
}

Class:
public class LaptopFactory
{
public static Laptop getSpecs(String type, int ram, int ssd, String cpu)
{
if("min".equalsIgnoreCase(type))
{
return new Minimum(ram, ssd, cpu);
}
else if("reco".equalsIgnoreCase(type))
{
return new Recommended(ram, ssd, cpu);
}
return null;
}
}

Main Method:
public class TestFactory
{
public static void main(String[] args)
{
Laptop min = LaptopFactory.getSpecs("min", 8, 256, "i5-12450Hz");
Laptop reco = LaptopFactory.getSpecs("reco", 8, 512, "i7-12700Hz");

System.out.println("Minimum Specs: " + min);


System.out.println("Recommended Spces: " + reco);
}
}

Output:

You might also like