Solved Homework: Question: Create an application that calculates the area and perimeter of a rectangle and validates the use…

Create an application that calculates the area and perimeter of a rectangle and validates the user input.

Be sure to test your program logic completely to be sure all program decisions work correctly.

Example Console Display (use this given test data for your application.)

Welcome to the My Calculator Application

Enter length: ten

Error! Invalid decimal value. Try again.

Enter length: -10

Error! Number must be greater than 0.0

Enter length: 100000000000000000000000

Error! Number must be less than 1000000.0

Enter length: 100

Enter width: what?

Error! Invalid decimal value. Try again.

Enter width: 0

Error! Number must be greater than 0.0

Enter width: 10000000000000000000000

Error! Number must be less than 1000000.0

Enter width: 100

Area:         10000.0

Perimeter:    400.0

Continue? (y/n): Note: no value was entered by the user at this prompt

Error! This entry is required. Try again.

Continue? (y/n): x

Error! Entry must be ‘y’ or ‘n’. Try again.

Continue? (y/n): n

Specifications

Use proper statement indentation and meaningful variable names in the code.

Add a multi-line description of this application (to include your name and the date written) at the beginning of the code for other programmers to read.

Add a comment to each code statement explaining why the statement is in the code.

If the application requires a numeric value, the application should continue prompting the user until the user enters a valid number.

If the application requires a string value, it should continue prompting the user until the user enters a valid string value.

If the user enters without typing in any value, the application should continue prompting the user until the user enters a value.

The code that’s used to validate data must be written in separate methods.

The project will consist of one class with several methods in addition to the main method to receive full credit.

Expert Answer

Dear Student,

Please find below the code which handles all the scenarios. Also I have provided the logic in separate methods and comments. Hope you receive a full credit.

If you like this answer, please give a thumbs up!

package com.assignment.distributedComputing;

import java.util.*;

public class JavaAreaAndPerimeterOfRectangle {

public static void main(String args[]) {

//variable declaration

int length = 0, width=0, area=0, perimeter=0;

boolean flag=false; //flag variable to determine continue/exit choice from user

System.out.println(“Welcome to the My Calculator Application”);

do{

//initialize scanner

Scanner s = new Scanner(System.in);

while(true){

System.out.println(“Enter Length”);

String str = s.next(); //take user input

try{

length = Integer.parseInt(str); //convert the user input to integer from string

if(length > 1000000){ //check if lenght is greater than 1000000

System.out.println(“Error! Number must be less than 1000000.0”);

}

else if (length<=0){ //check if length is negative

System.out.println(“Error! Number must be greater than 0.0”);

}

else{ // break the loop – meaning the length is correctly entered by user

break;

}

}catch (NumberFormatException ex) { // If user types wrong input

System.out.println(“Error! Invalid decimal value. Try again.”);

}

}

while(true){

System.out.println(“Enter Width :”);

String str = s.next();

try{

width = Integer.parseInt(str);

if(width > 1000000){ //check if lenght is greater than 1000000

System.out.println(“Error! Number must be less than 1000000.0”);

}

else if (width<=0){ //check if length is negative

System.out.println(“Error! Number must be greater than 0.0”);

}

else{ // break the loop – meaning the width is correctly entered by user

break;

}

}catch (NumberFormatException ex) {// If user types wrong input

System.out.println(“Error! Invalid decimal value. Try again.”);

}

}

findArea(length,width); //call method to print area

findPeremeter(length,width); //call method to print peremeter

while(true){

System.out.println(“Continue? (y/n)”); //ask user to enter choice y/n

String str = s.next();

if (str.equals(“n”)){ //if user enters n then we should terminate the program

flag = true; //set the flag to true

break;

}

else if(str.equals(“y”)){ //if user wants to continue

break;

}

else{ //if user enters some other characters

System.out.println(“Error! Entry must be ‘y’ or ‘n’. Try again.”);

}

}

}while(flag!=true);

}

//method to print parameter

private static void findPeremeter(int length, int width) {

System.out.println(“perimeter :”+ 2 * (length + width));

}

//method to print area

private static void findArea(int length, int width) {

System.out.println(“Area :”+ length * width);

}

}

Sample Output :

Welcome to the My Calculator Application

Enter length: ten

Error! Invalid decimal value. Try again.

Enter length: -10

Error! Number must be greater than 0.0

Enter length: 100000000000000000000000

Error! Number must be less than 1000000.0

Enter length: 100

Enter width: what?

Error! Invalid decimal value. Try again.

Enter width: 0

Error! Number must be greater than 0.0

Enter width: 10000000000000000000000

Error! Number must be less than 1000000.0

Enter width: 100

Area:         10000.0

Perimeter:    400.0

Continue? (y/n): x

Error! Entry must be ‘y’ or ‘n’. Try again.

Continue? (y/n): n

Happy Learning 🙂

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?