Concept explainers
Geometry Calculator
Design a Geometry class with the following methods:
- A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula:
Area = πr2
Use Math.PI for π and the radius of the circle for r.
- A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the following formula:
Area = Length × Width
- A static method that accepts the length of a triangle’s base and the triangle’s height. The method should return the area of the triangle. Use the following formula:
Area = Base × Height × 0.5
The methods should display an error message if negative values are used for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height.
Next, write a program to test the class, which displays the following menu and responds to the user’s selection:
Geometry Calculator
- 1. Calculate the Area of a Circle
- 2. Calculate the Area of a Rectangle
- 3. Calculate the Area of a Triangle
- 4. Quit
Enter your choice (1-4):
Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu.
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (8th Edition)
Problem Solving with C++ (10th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
C++ How to Program (10th Edition)
C Programming Language
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
- Charge Account Validation Using Java programming Create a class with a method that accepts a charge account number as its argument. The method should determine whether the number is valid by comparing it to the following list of valid charge account numbers:5658845 4520125 7895122 8777541 8451277 13028508080152 4562555 5552012 5050552 7825877 12502551005231 6545231 3852085 7576651 7881200 4581002These numbers should be stored in an array. Use a sequential search to locate the number passed as an argument. If the number is in the array, the method should return true, indicating the number is valid. If the number is not in the array, the method should return false, indicating the number is invalid.Write a program that tests the class by asking the user to enter a charge account number. The program should display a message indicating whether the number is valid or invalid.arrow_forwardpython Cylinder class: Instance variables: Make instance variables appropriately to accomplish the tasks you need. Methods: ?_init_ :: constructor, initializes instance variables o Additional Parameters: diameter and height (in that order) o Assumption: diameter and height will always be numbers which represent measurements in millimeters o Temporary Assumption (we will fix this later): diameter will never be negative and height wilI always be positive number above o ? get_volume : returns the volume of the cylinder based on its height o Additional Parameters: No additional parameters (just self). .0 Note: Use pi from the math module ?_str__ :: returns string representation with this format: "Cylinder (radius: 20.25mm, thickness: 6.10mm, volume: 7858.32mm^3)" o Additional Parameters: No additional parameters. o Notes: ? The quotes in the example are just our string boundaries! The first character in the above example is C. ? This method reports the radius (NOT the diameter). ? All…arrow_forwardAssignment: Dice Rolling Program Objective: Create a Java program that rolls two dice and displays the results. The program should have two Java classes: one for a single die and another for a pair of dice. Assignment Details: User Input: Ask the user to specify the number of sides they want on each die. Ensure that the user's input is within a reasonable range. Dice Rolling: Simulate rolling the dice using Math.random() based on the user's chosen number of sides. Display the sum of the values rolled, e.g., "5 + 3 = 8." Special Combinations: If the dice roll results in combinations of 2, 7, or 12, print special messages: "1 + 1 = 2 snake eyes!" "3 + 4 = 7 craps!" "6 + 6 = 12 box cars!" Main Method: In the main method, create a pair of dice, roll them, and display the results. Allow the user to decide whether to continue rolling the dice or exit the program. Additional Features: You are welcome to add more features or enhancements to the program if desired. In…arrow_forward
- In PYTHON Design a class named Car that has the following fields: yearModel: The yearModel field is an integer that holds the car's year model (e.g, 1959). speed: The speed field is an integer that holds the car's current speed. In addition, the class should have the following constructor and other methods: Constructor: The constructor should accept the car's year model and speed as arguments. These values should be assigned to the object's yearModel and speed fields.The constructor should also assign 0 to the speed field. Accessors: Design appropriate accessor methods to get the values stored in an object's yearModel and speed fields. accelerate: The accelerate method should add 5 to the speed field each time is called. brake: The brake method should subtract 5 from the speed field each time it is called. Next, design a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed ofthe car and…arrow_forwardPrime Numbers A prime number is a number that can be evenly divided by only itself and 1. For example, the number 5 is prime because it can be evenly divided by only 1 and 5. The number 6, however, is not prime because it can be evenly divided by 1, 2, 3, and 6. Write a Boolean method named IsPrime that takes an integer as an argument and returns true if the argument is a prime number or false otherwise. Use the method in an application that lets the user enter a number and then displays a message indicating whether the number is prime.arrow_forwardQuestionarrow_forward
- Incorrect Question 6 public class Shape { Color fillColor; } public Shape() { fill Color = Color. RED; } } public Shape(Color fillColor) { this.fill Color = fillColor; } public class Circle extends Shape { int radius; public Circle() { radius = 1; } public Circle(int radius) { this.radius = radius; } public Circle(int radius, Color fillColor) { } super(fillColor); this.radius = radius; Circle c1= new Circle(); Circle c2= new Circle(5); Circle c3 = new Circle(10, Color.BLUE); Which of the following are correct? c2 has radius 5 c1 is red c2 is red c3 has radius 10 c3 has radius 1 c3 is red c1 is blue ✔c2 has radius 10 c3 is blue c1 has radius 5 c1 has radius 1 c2 has radius 1 c2 is bluearrow_forwardJava programarrow_forwardAt most, a class can contain ____________ method(S). 0 1 2 any number ofarrow_forward
- True or False: Between method calls, the value of a local variable is preserved.arrow_forwardMethods with an empty parameter list and do not return a value: [Questions 1-8 required] You invoke a method by its name followed by a pair of brackets and the usual semi-colon Write a method called DisplayPersonalInfo(). This method will display your name, school, program and your favorite course. Call the DisplayPersonalInfo() method from your program Main() method Write a method called CalculateTuition(). This method will prompt the user for the number of courses that she is currently taking and then calculate and display the tuition cost. (cost = number of course * 569.99). Call the CalculateTuition() method two times from the same Main() method as in question 1. Write a method call CalculateAreaOfCircle(). This method will prompt the user for the radius of a circle and then calculate and display the area.[A = πr2].Call the CalculateAreaOfCircle() method twice from the same Main() method as in question 1. Use Math.Pi for the value of π Write a method call…arrow_forwardTopics Classes Methods Data Collections: Lists, Tuples, and Dictionaries String Manipulation Chess Objective: practicing with classes, instance methods, data collections, loops, if and elif statements, and string methods Description In this assignment you will write a program that shows the valid moves of chess pieces. Your program will draw a board with 64 squares using the traditional layout, next ask the user to choose a move, and then, depending on the user's choice, redraw the board with the selected chess piece and its valid moves. Please see the examples of valid moves of chess pieces and the traditional chess board layout below: At the beginning, your program should draw an empty chess board and prompt the user to enter a move: Welcome to the Chess Game! a b c d e f g h +---+---+---+---+---+---+---+---+8| | | | | | | | |8 +---+---+---+---+---+---+---+---+7| | | | | | | | |7 +---+---+---+---+---+---+---+---+6| | | | | |…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,