Question

Programmed in Java, please use inheritance, as that is the part i'm confused on. Thanks! For...

Programmed in Java, please use inheritance, as that is the part i'm confused on. Thanks!

For this lab you are creating 4 classes:

  1. Main class

Has the main method and tests the other classes.

  1. Computer
    1. We are storing the following information about our computers
      1. CPU type
      2. CPU speed
      3. RAM size
  2. Laptop
    1. We are storing the following information about our laptops
      1. All the information we have in the computer
      2. Battery life in hours
      3. The current battery level (hours remaining).
    2. Charge method that sets the battery level to the max level.
  3. Desktop
    1. We are storing the following information about our laptops
      1. All the information we have in the computer
      2. Number of screens

Write the appropriate constructors, getters and setters in each class.

In your main class create a laptop with an I7 10 generation 3.5 GHZ CPU, 16 GB of RAM and a battery that lasts 10 hours.

You will also need to create a Desktop with the same specs that has 2 screens.

Use the toString method to print the details for your laptop and desktop.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Computer.java

======================

package computer;

public class Computer {
   // Parent class
   // other class derived from this class
  
   // data members
   private String CPU_type; // type of CPU
   private double CPU_speed; // CPU speed in GHZ
   private int RAM_size; // size of RAM in GB
  
   // default constructor
   public Computer() {
       // initialize data members
       CPU_type = "";
       CPU_speed = 0;
       RAM_size = 0;
   }
  
   // constructor with parameters
   public Computer(String CPU_type, double CPU_speed, int RAM_size) {
       // initialize data members
       // this indicates current instance of class,
       // it is used to make parameter name distinguish from member data names
       this.CPU_type = CPU_type;
       this.CPU_speed = CPU_speed;
       this.RAM_size = RAM_size;
   }

   // getters
   public String getCPUType() {
       return CPU_type;
   }
  
   public double getCPUSpeed() {
       return CPU_speed;
   }
  
   public int getRAMSize() {
       return RAM_size;
   }
  
   // setters
   public void getCPUType(String CPU_type) {
       this.CPU_type = CPU_type;
   }
  
   public void getCPUSpeed(double CPU_speed) {
       this.CPU_speed = CPU_speed;
   }
  
   public void getRAMSize(int RAM_Size) {
       this.RAM_size = RAM_Size;
   }
  
   // returns all data members in string form
   public String toString() {
       String str = "CPU Type: " + CPU_type + "\nCPU Speed: " + CPU_speed + " GHZ\nRAM Size: " + RAM_size + " GB\n";
       return str;
   }
}

=============================

Laptop.java

=============================

package computer;

public class Laptop extends Computer{
   // child class of Computer class
  
   // data members
   private int batteryLife; // Battery life in hours
   private int batteryLevel; // The current battery level (hours remaining)

   // default constructor
   public Laptop() {
       super();
       batteryLife = 0;
       batteryLevel = batteryLife;
   }
  
   // constructor with parameters
   public Laptop(String CPU_type, double CPU_speed, int RAM_size, int batteryLife) {
       // initialize data members
       // call to parent class constructor to initialize its data
       super(CPU_type, CPU_speed, RAM_size);
       this.batteryLife = batteryLife;
       batteryLevel = batteryLife; // Laptops sold are fully charged
   }
  
   // getters
   public int getBatteryLife() {
       return batteryLife;
   }
  
   public int getBatteryLevel() {
       return batteryLevel;
   }

   // setters
   public void setBatteryLife(int batteryLife) {
       this.batteryLife = batteryLife;
   }
  
   public void setBatteryLevel(int batteryLevel) {
       this.batteryLevel = batteryLevel;
   }
  
   // sets the battery level to the max level
   public void charge() {
       setBatteryLevel(batteryLife);
   }
  
   // returns all data members in string form
   public String toString() {
       String str = "Laptop: \n" + super.toString() // call to parent class's to string method to print its data first
           + "Battery Life: " + batteryLife + " Hrs\n" + "Current Bettery Level: " + batteryLevel + " Hrs\n";
       return str;
   }
}

=================================

Desktop.java

=================================

package computer;

public class Desktop extends Computer{
   // child class of Computer class
  
   // data members
   private int screens; // Number of screens
  
   // default constructor
   public Desktop() {
       super();
       screens = 0;
   }
  
   // constructor with parameters
   public Desktop(String CPU_type, double CPU_speed, int RAM_size, int screens) {
       // initialize data members
       // call to parent class constructor to initialize its data
       super(CPU_type, CPU_speed, RAM_size);
       this.screens = screens;
   }
  
   // getters
   public int getScreens() {
       return screens;
   }
  
   // setters
   public void setScreens(int screens) {
       this.screens = screens;
   }
  
   // returns all data members in string form
   public String toString() {
       String str = "Desktop: \n" + super.toString() // call to parent class's to string method to print its data first
           + "Number of Screens: " + screens + "\n";
       return str;
   }
  
}

=========================

Tester.java

=========================

package computer;

public class Tester {
  
   // main method to run the program
   public static void main(String[] args) {
       // create a laptop with an I7 10 generation 3.5 GHZ CPU, 16 GB of RAM and a battery that lasts 10 hours
       Laptop laptop = new Laptop("I7 10 generation", 3.5, 16, 10);
      
       // create a Desktop with the same specs that has 2 screens
       Desktop desktop = new Desktop("I7 10 generation", 3.5, 16, 2);
      
       // print the details for laptop and desktop
       System.out.println(laptop);
       System.out.println(); // print empty line
      
       System.out.println(desktop);
   }

}

let me know if you have any problem or doubts. thank you.

Add a comment
Know the answer?
Add Answer to:
Programmed in Java, please use inheritance, as that is the part i'm confused on. Thanks! For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

    write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...

  • Please include comments in java Create a command line program that can be used for computer...

    Please include comments in java Create a command line program that can be used for computer inventory purposes Create a super class that will store the following data: CPU speed Amount of RAM Hard Disk size Operating System Allow the user to enter data using any units of measurement they desire (i.e. - 500GB or 1TB for hard disk size). Create a class that inherits from the previous class and stores information for a Windows based computer. Store the following...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Description: You have been asked to help the College of IST Running Club by designing a...

    Description: You have been asked to help the College of IST Running Club by designing a program to help them keep track of runners in the club, the races that will occur, and each runner's performance in each race! Requirements: Please use Java to implement the program Your program must: Store information about each Runner. Store information about each Race. Store information about the performance of each runner in each race that they compete in. Note that each race may...

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • in java : Create Java Application you are to create a project using our designated IDE...

    in java : Create Java Application you are to create a project using our designated IDE (which you must download to your laptop). Create the code to fulfill the requirements below. Demonstrate as stipulated below. Create a Main Application Class called AddressBookApplication (a counsole application / command line application). It should Contain a Class called Menu that contains the following methods which print out to standard output a corresponding prompt asking for related information which will be used to update...

  • Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal...

    Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT