Question

What is wrong with this Code? Fix the code errors to run correctly without error. There...

What is wrong with this Code? Fix the code errors to run correctly without error. There are two parts for one code (one question) the two parts branch off, one part has 2 sheets of code, the other has 3 sheets of code, all are small code segments for one question.

Pt.1 - 2 sheets of code:

public class Computer {

private String make; private String type; private String modelNumber;
private double cpuSpeed_GHz; private int ramSize_GB;
private int hardDriveSize_GB; private double price;

public Computer(String make, String type, String modelNumber,
double cpuSpeed_GHz, int ramSize_GB,
int hardDriveSize_GB, double price){

make = make;
type = type;
modelNumber = modelNumber;
cpuSpeed_GHz = cpuSpeed_GHz;
ramSize_GB = ramSize_GB;
hardDriveSize_GB = hardDriveSize_GB;
price = price;
}

public double getCpuSpeed_GHz() {
return cpuSpeed_GHz;
}

public int getHardDriveSize_GB() {
return hardDriveSize_GB;
}

public String getMake() {
return make;
}

public String getModelNumber() {
return modelNumber;
}

public double getPrice() {
return price;
}

public int getRamSize_GB() {
return ramSize_GB;
}

public String getType() {
return type;
}

public void setCpuSpeed_Ghz(double cpuSpeed_GHz) {
this.cpuSpeed_GHz = cpuSpeed_GHz;
}

public void setHardDriveSize_GB(int hardDriveSize_GB) {
this.hardDriveSize_GB = hardDriveSize_GB;
}

public void setMake(String make) {
this.make = make;
}

public void setModelNumber(String modelNumber) {
this.modelNumber = modelNumber;
}

public void setPrice(double price) {
this.price = price;
}

public void setRamSize_GB(int ramSize_GB) {
this.ramSize_GB = ramSize_GB;
}

public void setType(String type) {
this.type = type;
}
}

public class ComputerTest {
public static void main(String[] args){
Computer computer = new Computer("Dell", "Laptop", "Inspiron 17R",
2.3, 4, 500, 1200.50);
  
System.out.printf("%s", computer.getMake());
System.out.printf(" %s", computer.getType());
System.out.printf(" %s\n", computer.getModelNumber());
System.out.printf("%.2f GHz CPU,", computer.getCpuSpeed_GHz());
System.out.printf(" %d GB Memory\n", computer.getRamSize_GB());
System.out.printf("%d GB Hard Drive\n", computer.getHardDriveSize_GB());
System.out.printf("Cost $%,.2f\n", computer.getPrice());
}
}

Pt.2 - 3 sheets of code

public class BigScientificMathProgram {
public void generateSomeBigScientificCalcuation(){
System.out.printf("The speed of light is [m/s]: %,d\n",
ScienceMathConstants.SPEED_OF_LIGHT_METERS_PER_SECONDS);
System.out.printf("The speed of sound is [km/hr]: %,d\n",
ScienceMathConstants.SPEED_OF_SOUND_KILOMETERS_PER_HOUR);
System.out.printf("The distance from the earth to the sun is [kilo]: %,d\n",
ScienceMathConstants.DISTANCE_FROM_EARTH_TO_SUN_KILOMETERS);
}
}

public class BigScientificMathProgramTest {
public static void main(String[] args) {
BigScientificMathProgram bsmp = new BigScientificMathProgram();
bsmp.generateSomeBigScientificCalcuation();
}
}

public class ScienceMathConstants {
public int SPEED_OF_LIGHT_METERS_PER_SECONDS = 299792458; // meters per second
public int SPEED_OF_SOUND_KILOMETERS_PER_HOUR = 1236; // kilometres per hour
public int DISTANCE_FROM_EARTH_TO_SUN_KILOMETERS = 150000000; // kilometers
}

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

Part 1: BigScientificMathProgram

In this part, there is an error in class ScienceMathConstants. In this, we are using class variables in the main method which is a static method and we can't access not static objects from static methods.

To fix this, we need to make all variables in ScienceMathConstants as static.

Below is the complete code of Part 1 where the updated code has been highlighted in bold.

public class ScienceMathConstants {

    // static variables
    public static int SPEED_OF_LIGHT_METERS_PER_SECONDS = 299792458; // meters per second
    public static int SPEED_OF_SOUND_KILOMETERS_PER_HOUR = 1236; // kilometres per hour
    public static int DISTANCE_FROM_EARTH_TO_SUN_KILOMETERS = 150000000; // kilometers
}

public class BigScientificMathProgram {

    public void generateSomeBigScientificCalcuation() {
        System.out.printf("The speed of light is [m/s]: %,d\n",
                ScienceMathConstants.SPEED_OF_LIGHT_METERS_PER_SECONDS);
        System.out.printf("The speed of sound is [km/hr]: %,d\n",
                ScienceMathConstants.SPEED_OF_SOUND_KILOMETERS_PER_HOUR);
        System.out.printf("The distance from the earth to the sun is [kilo]: %,d\n",
                ScienceMathConstants.DISTANCE_FROM_EARTH_TO_SUN_KILOMETERS);
    }
}

public class BigScientificMathProgramTest {

    public static void main(String[] args) {
        BigScientificMathProgram bsmp = new BigScientificMathProgram();
        bsmp.generateSomeBigScientificCalcuation();
    }
}

Below is the sample output:

run: The speed of light is [m/s] 299,792 , 458 The speed of sound is [km/hr]: 1,236 The distance from the earth to the sun is

Part 2 - Computer

This is not generating the correct output for computer details. This is happening because in constructor of Computer class, we are assigning the value of variables to itself. To fix this, we need to make it to refer object variables using this keyword.

Below is the complete code of Part 2 where the updated code has been highlighted in bold.


public class Computer {

    private String make;
    private String type;
    private String modelNumber;
    private double cpuSpeed_GHz;
    private int ramSize_GB;
    private int hardDriveSize_GB;
    private double price;

    public Computer(String make, String type, String modelNumber,
            double cpuSpeed_GHz, int ramSize_GB,
            int hardDriveSize_GB, double price) {

        this.make = make;
        this.type = type;
        this.modelNumber = modelNumber;
        this.cpuSpeed_GHz = cpuSpeed_GHz;
        this.ramSize_GB = ramSize_GB;
        this.hardDriveSize_GB = hardDriveSize_GB;
        this.price = price;
    }

    public double getCpuSpeed_GHz() {
        return cpuSpeed_GHz;
    }

    public int getHardDriveSize_GB() {
        return hardDriveSize_GB;
    }

    public String getMake() {
        return make;
    }

    public String getModelNumber() {
        return modelNumber;
    }

    public double getPrice() {
        return price;
    }

    public int getRamSize_GB() {
        return ramSize_GB;
    }

    public String getType() {
        return type;
    }

    public void setCpuSpeed_Ghz(double cpuSpeed_GHz) {
        this.cpuSpeed_GHz = cpuSpeed_GHz;
    }

    public void setHardDriveSize_GB(int hardDriveSize_GB) {
        this.hardDriveSize_GB = hardDriveSize_GB;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public void setModelNumber(String modelNumber) {
        this.modelNumber = modelNumber;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setRamSize_GB(int ramSize_GB) {
        this.ramSize_GB = ramSize_GB;
    }

    public void setType(String type) {
        this.type = type;
    }
}

Below is the output after fixing the code:

run: Dell Laptop Inspiron 17R 2.30 GHz CpU, 4 GB Memory 500 GB Hard Drive Cost $1,200.50

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
What is wrong with this Code? Fix the code errors to run correctly without error. There...
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
  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

  • For the below code, what will be printed? public class mathchar { public static void main(String[]...

    For the below code, what will be printed? public class mathchar { public static void main(String[] args) { int i = 81, j = 3, k = 6; char w = 'f'; System.out.printf("%.2f\n", Math.sqrt(i)); System.out.printf("%.2f\n", Math.pow(j,k)); System.out.printf("%c\n", Character.toUpperCase(w)); System.out.printf("%c\n", Character.toLowerCase(w)); System.out.printf("%d\n", (int)(Math.random() * 21 + 6)); /* just tell range of possible values */ } }

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • I need help in getting the second output to show both the commission rate and the...

    I need help in getting the second output to show both the commission rate and the earnings for the String method output package CompemsationTypes; public class BasePlusCommissionEmployee extends CommissionEmployee { public void setGrossSales(double grossSales) { super.setGrossSales(grossSales); } public double getGrossSales() { return super.getGrossSales(); } public void setCommissionRate(double commissionRate) { super.setCommissionRate(commissionRate); } public double getCommissionRate() { return super.getCommissionRate(); } public String getFirstName() { return super.getFirstName(); } public String getLastName() { return super.getLastName(); } public String getSocialSecurityNumber() { return super.getSocialSecurityNumber(); } private...

  • Provided code Animal.java: public class Animal {    private String type;    private double age;   ...

    Provided code Animal.java: public class Animal {    private String type;    private double age;       public Animal(String aT, double anA)    {        this.type = aT;        if(anA >= 0)        {            this.age = anA;        }    }    public String getType()    {        return this.type;    }    public double getAge()    {        return this.age;    } } Provided code Zoo.java: public class Zoo {...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • Hello everybody. Below I have attached a code and we are supposed to add comments explaining...

    Hello everybody. Below I have attached a code and we are supposed to add comments explaining what each line of code does. For each method add a precise description of its purpose, input and output.  Explain the purpose of each member variable. . Some help would be greaty appreciated. (I have finished the code below with typing as my screen wasnt big enough to fit the whole code image) } public void withdrawal (double amount) { balance -=amount; }...

  • I have given you a piece of code to test that your circle class works correctly....

    I have given you a piece of code to test that your circle class works correctly. Add your circle class to the code. public class Lab2Num1 { public static class Circle { private double radius; //your code goes here    //provide default constructor, constructor with one parameter, area, and circumference    } public static void main(String[] args) { Circle c = new Circle(1.5); System.out.printf("The circumference of a circle of radius " + c.getRadius()+ " is %5.2f\n", c.circumference()); System.out.printf("The area of...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

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