Question

In: Computer Science

Question1: Consider the below description of the class Box. 1. Create a class and call it...

Question1: Consider the below description of the class Box. 1. Create a class and call it Box. Box has the following methods & variables a. Variable for length, height, width & volume, b. setlength(int x ) method to set the length, c. setWidth (int y ) method to set the width, d. setHeight (int z ) method to set the height & e. displayCalculatedVolume( ) method that calculates the volume and displays it. (Hint: volume = length*width*height)

Solutions

Expert Solution

Answer:

Here is the code, please go through the comments for better understanding of the code. Drop me a comment if you have any doubts, I'll be glad to help you. All you have to do is to create a class called Box.java, paste the below code and run, Enjoy!

Box.java

public class Box { // start of class

// attributes of the box
private int length;
private int height;
private int width;
private int volume;

public void setLength(int length) { // this method sets the length
this.length = length;
}

public void setHeight(int height) { // this method sets the height
this.height = height;
}

public void setWidth(int width) { // this method sets the width
this.width = width;
}

public void displayCalculatedVolume(){ // this method calculates the volume
volume = length*width*height;
System.out.println("The volume of the box : " + volume); // displaying the volume
}
public static void main(String [] args){ // start of main()
Box b = new Box(); // create an instance of class Box
b.setLength(10); // call the method setLength() with some int value
b.setHeight(8); // call the method setHeight() with some int value
b.setWidth(5); // call the method setWidth() with some int value
b.displayCalculatedVolume(); // calculates the volume with the given values
} // end of main()
} // end of class

Output's screenshot


Related Solutions

Create a sub class of the class Box and call it SBox. The SBox class has...
Create a sub class of the class Box and call it SBox. The SBox class has inherited the list of variables and methods from Box & has its own members too. a. It has a variable color having a String data type. b. This class also has a method called SetColor( ) which read the color from the user (Use JOptionPane), set the color to the value given by the user and display it.
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
C++ Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program...
C++ Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program to test your class. Use integer variables to represent the private data of the class, meaning the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example fraction 2/4 would be stored...
Challenge: Dog Description: Create a Dog class that contains specified properties and methods. Create an instance...
Challenge: Dog Description: Create a Dog class that contains specified properties and methods. Create an instance of Dog and use its methods. Purpose: This application provides experience with creating classes and instances of objects in C#. Requirements: Project Name: Dog Target Platform: Console Programming Language: C# Documentation: Types and variables (Links to an external site.) (Microsoft) Classes and objects (Links to an external site.) (Microsoft) Enums (Links to an external site.) (Microsoft) Create a class called Dog. Dog is to...
Use Javascript to implement the class below to the code below. Create a class Order which...
Use Javascript to implement the class below to the code below. Create a class Order which contains one array member variable that stores Sandwich objects. Initially, this array is empty. Add a function named add that adds a sandwich to the array. Add a getter function named price which iterates over the array and sums up the price of each  sandwich in the array. class Sandwich { constructor(price) { this.price = price; } toString() { return "Sandwich", this.price; } } class...
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
The following code is for a class named Box. The class Box includes a constructor method...
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume(). For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but...
C-coding Question1: Fill in the function body for the provided sum() function such that the call...
C-coding Question1: Fill in the function body for the provided sum() function such that the call sum(g, i, j) returns the value of the calculation: Note: For the case where j < i, the function sum() should return 0. #include <stdio.h> int g(int val) { return val * val; } int sum(int (*f)(int val), int start, int end) { /* Your code goes here */ } int main() { printf("Result: %d\n", sum(g, 10, 20)); return 0; } ****************************************************** Question 2:...
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class BankCharges { public static void main(String[] args) { //Variable declarations int numChecks; double perCheckFee; double totalFee; double totalFeesAllAccounts = 0; //accumulator int numAccounts;    //Constant declarations for base fee and per check fees final double BASE_FEE = 10.0; final double LESS_THAN_20_FEE = 0.10; final double TWENTY_TO_THIRTYNINE_FEE = 0.08; final double FORTY_TO_FIFTYNINE_FEE = 0.06; final double SIXTY_OR_MORE_FEE = 0.04;    // Create a Scanner...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT