Adding functionality
- Eclipse: Oxygen
- Java: 1.8
Adding functionality
This tutorial deals with describing various functionalities of Composition. As described earlier in composition we can ‘inherit’ objects which are used to reference the method or the attribute related to the class.
The same concept is described here when the attributes of processor class are accessed by the Demo class via Laptop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
public class Laptop { private float screen; private Processor processor; private String ram; private String hardDrive; private GraphicsCard graphicsCard; private String opticalDrive; private String keyboard; public Laptop() { this.screen = 15.6f; this.processor = new Processor(); this.ram = "DDR4"; this.hardDrive = "2TB"; this.graphicsCard = new GraphicsCard(); this.opticalDrive = "MLT layer"; this.keyboard = "backlit"; } public Laptop(float screen, Processor processor, String ram, String hardDrive, GraphicsCard graphicsCard, String opticalDrive, String keyboard) { this.screen = screen; this.processor = processor; this.ram = ram; this.hardDrive = hardDrive; this.graphicsCard = graphicsCard; this.opticalDrive = opticalDrive; this.keyboard = keyboard; } @Override public String toString() { return "Laptop [screen=" + screen + ", processor=" + processor + ", ram=" + ram + ", hardDrive=" + hardDrive + ", graphicsCard=" + graphicsCard + ", opticalDrive=" + opticalDrive + ", keyboard=" + keyboard + "]"; } public Processor getProcessor() { return processor; } public float getScreen() { return screen; } public String getRam() { return ram; } public String getHardDrive() { return hardDrive; } public GraphicsCard getGraphicsCard() { return graphicsCard; } public String getOpticalDrive() { return opticalDrive; } public String getKeyboard() { return keyboard; } public String gamingMode(){ processor.setFrequency(processor.getMaxfrequency()); return "Success"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class GraphicsCard { private String brand; private int series; private String memory; public GraphicsCard() { this.brand = "Nvidia"; this.series = 940; this.memory = "2 GB"; } public GraphicsCard(String brand, int series, String memory) { this.brand = brand; this.series = series; this.memory = memory; } @Override public String toString() { return "GraphicsCard [brand=" + brand + ", series=" + series + ", memory=" + memory + "]"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
public class Processor { private String brand; private String series; private int generation; private int cores; private int threads; private String cacheMemory; private String frequency; private String minFrequency; private String maxfrequency; public Processor() { this.brand = "intel"; this.series = "i5 7200u"; this.generation = 7; this.cores = 2; this.threads = 4; this.cacheMemory = "3MB"; this.frequency = "2.5Ghz"; this.minFrequency = "2.5Ghz"; this.maxfrequency = "3.1Ghz"; } public Processor(String brand, String series, int generation, int cores, int threads, String cacheMemory, String frequency, String minFrequency, String maxfrequency) { this.brand = brand; this.series = series; this.generation = generation; this.cores = cores; this.threads = threads; this.cacheMemory = cacheMemory; this.frequency = frequency; this.minFrequency = minFrequency; this.maxfrequency = maxfrequency; } @Override public String toString() { return "Processor [brand=" + brand + ", series=" + series + ", generation=" + generation + ", cores=" + cores + ", threads=" + threads + ", cacheMemory=" + cacheMemory + ", frequency=" + frequency + ", minFrequency=" + minFrequency + ", maxfrequency=" + maxfrequency + "]"; } public String getBrand() { return brand; } public String getSeries() { return series; } public int getGeneration() { return generation; } public int getCores() { return cores; } public int getThreads() { return threads; } public String getCacheMemory() { return cacheMemory; } public String getFrequency() { return frequency; } public String getMinFrequency() { return minFrequency; } public String getMaxfrequency() { return maxfrequency; } public void setFrequency(String frequency) { this.frequency = frequency; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Hello { public static void main(String[] args) { //Laptop lappy = new Laptop(); //System.out.println(lappy.getProcessor().getBrand()); Processor processor = new Processor("intel", "7200U", 7, 4, 4, "6MB" + "", "2.5Ghz", "2.5Ghz", "3.1Ghz"); GraphicsCard graphicsCard = new GraphicsCard("Nvidia", 1050, "4GB"); Laptop gamingLaptop = new Laptop(17f, processor, "DDR4", "2TB", graphicsCard, null, "backlit"); System.out.println(gamingLaptop); gamingLaptop.gamingMode(); System.out.println("Gaming mode on"); System.out.println("Current frequency: "+gamingLaptop.getProcessor().getFrequency()); } } |
Output
Laptop [screen=17.0, processor=Processor [brand=intel, series=7200U, generation=7, cores=4, threads=4, cacheMemory=6MB, frequency=2.5Ghz, minFrequency=2.5Ghz, maxfrequency=3.1Ghz], ram=DDR4, hardDrive=2TB, graphicsCard=GraphicsCard [brand=Nvidia, series=1050, memory=4GB], opticalDrive=null, keyboard=backlit]
Gaming mode on
Current frequency: 3.1Ghz
Contributed by: Poonam Tomar