Question

Hi, I was wondering if I could get some help on a java program that I...

Hi, I was wondering if I could get some help on a java program that I am supposed to be writing. The code that I have can be found below. Thanks in advance.

Here are the requirements of the code

       1) Create 3 cube objects. The size of each of the cubes
       should be input from the keyboard
       (hint: study the code below)
    2) Print the Side length, Surface area and Volume to the
       users screen (console) for each of the cubes
    3) The program should catch input error exceptions and
      deal with them in a reasonable manner
import java.util.Scanner;
import java.io.*;
 
public class Cube {
    // Hint: put your class variables and objects here
    // For example the Scanner statement as a 'static' object

    // Create the getLength() method to accept input and verify it is numeric

    // Create the calculateSurfaceArea() method

    // Create the calculateVolume() method
    
    /**
     * main() -- creates 3 instances of Cube and calculates the surface area and volume
     */
    public static void main(String args[]) throws IOException {
        int length;

        // HINT: input the side from the keyboard and check for errors and exceptions 
        Cube cube1 = new Cube(10);
        System.out.println("Side length of cube1 is " + cube1.getLength());
        System.out.println("Surface Area of cube1 is " + cube1.calculateSurfaceArea());
        System.out.println("Volume of cube1 is " +      cube1.calculateVolume());

        // Hint - add two more cube instances

        // Don't forget to close your Scanner object  
   } // main()
} // Cube
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN JAVA:

Cube.java file:

import java.io.IOException;

import java.util.Scanner;

public class Cube {

private double length;

public Cube(double length) {

this.length = length ;

}

public double getLength() {

return this.length;

}

public double calculateSurfaceArea() {

return 6*this.length*this.length;

}

public double calculateVolume() {

return this.length*this.length*this.length;

}

public static void main(String[] args) throws IOException{

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

double length1, length2, length3 ;

try {

//taking lengths of the cube as input from the user and creating objects to the Cube class

System.out.print("Enter length of cube1:");

length1 = sc.nextDouble();

Cube c1 = new Cube(length1);

System.out.print("Enter length of cube2:");

length2 = sc.nextDouble();

Cube c2 = new Cube(length2);

System.out.print("Enter length of cube3:");

length3 = sc.nextDouble();

Cube c3 = new Cube(length3);

//displaying the details of the cubes

System.out.println("\nDetails of Cube1:");

System.out.println("Length:"+c1.getLength());

System.out.println("Surface Area:"+c1.calculateSurfaceArea());

System.out.println("Volume:"+c1.calculateVolume());

System.out.println("\nDetails of Cube2:");

System.out.println("Length:"+c2.getLength());

System.out.println("Surface Area:"+c2.calculateSurfaceArea());

System.out.println("Volume:"+c2.calculateVolume());

System.out.println("\nDetails of Cube3:");

System.out.println("Length:"+c3.getLength());

System.out.println("Surface Area:"+c3.calculateSurfaceArea());

System.out.println("Volume:"+c3.calculateVolume());

}

catch(Exception e) {

System.out.println(e);

}

}

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Hi, I was wondering if I could get some help on a java program that I...
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
  • ******** IN JAVA ********* I have a program that I need help debugging. This program should...

    ******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...

  • I have this program: It passes on certain test cases. The Demo five has the %.1f...

    I have this program: It passes on certain test cases. The Demo five has the %.1f to cut down the number to one decimal place. But I get a failed attempt such as: Enter length: \n Enter width: \n You entered: 87.3, 2.0, and 174.7\n -- Rectangle info --\n Length: 87.34\n Width: 2.0\n Area: 174.68\n And I know it is because the length and area both have 2 decimal places. Could you help me fix this? Thank you. Program Content:...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Topic: Java Programming Hello, I have the following code attached below, and keep getting told that...

    Topic: Java Programming Hello, I have the following code attached below, and keep getting told that "Television" on the 23rd line "cannot be resolved to a type". It happens on this line of code... Television portable = new Television("Sharp", 19); Can you tell me how to fix this? Beginning of code... import java.util.Scanner; public class TelevisionDemo { {} public static void main(String[] args) { // //create a Scanner object to read from the keyboard Scanner keyboard = new Scanner(System.in); //...

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