Question

Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate...

Use Java program
Material Covered : Loops & Methods

Question 1: Write a program to calculate rectangle area. Some requirements:
1. User Scanner to collect user input for length & width
2. The formula is area = length * width
3. You must implement methods getLength, getWidth, getArea and displayData
▪ getLength – This method should ask the user to enter the rectangle’s length and then return that value as a double
▪ getWidth – This method should ask the user to enter the rectangle’s width and then return that value as a double
▪ getArea – This method should accept height and width as arguments, and return the rectangle’s area
▪ displayData – This method should accept height, width and area as arguments, and display the area with appropriate message on screen.

Question 2: Even/ Odd Counter
Write a program which has a method to check whether a number entered by the user is even or odd. Make sure you are using method evenOddCounter(). Ask user if they want to play again (Y/N)? if user types Y then the program should ask for next input and continue the program. However, if user types N then your program should stop execution.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 1

Program

import java.util.Scanner; //importing Scanner class
public class Main
{
static Scanner sc=new Scanner(System.in); //creating Scanner Object
static double getLength(){ //getLength method
System.out.print("Enter the length:");
double length=sc.nextDouble();
return length;
}
static double getWidth(){ //getWidth method
System.out.print("Enter the Width:");
double width=sc.nextDouble();
return width;
}
static double getArea(double length,double width){ //getArea method
double area=length*width;
return area;
}
static void displayData(double length,double width,double area){ //displayData method
System.out.println("Length of the rectangle:"+length);
System.out.println("Width of the rectangle:"+width);
System.out.println("Area of the rectangle:"+area);
}
   public static void main(String[] args) {
   //calling the methods
double l=getLength();
double w=getWidth();
double area=getArea(l,w);
displayData(l,w,area);
  
   }
}

Screenshot of the program

1 ها ہ import java.util.Scanner; //importing Scanner class 2 public class Main 3 -{ 4 static Scanner sc=new Scanner(System.in

Output

Enter the length:6 Enter the Width:7 Length of the rectangle:6.0 Width of the rectangle:7.0 Area of the rectangle:42.0

Question 2

Program

import java.util.Scanner; //importing Scanner class
public class Main
{
static Scanner sc=new Scanner(System.in); //creating Scanner Object
static void evenOddCounter(int num){ //evenOddCounter method
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
   public static void main(String[] args) {
   int num;
   String ch;
   do{
System.out.print("Enter the number:");
num=sc.nextInt();
evenOddCounter(num);
System.out.print("Do you want to play again (Y/N)? ");
ch=sc.next();
   }while(ch.equals("Y"));
  
   }
}

Screenshot of the program

1 import java.util.Scanner; //importing Scanner class 2 public class Main 3 - { 4 static Scanner sc=new Scanner(System.in); /

Output

Enter the number: 6 6 is even Do you want to play again (Y/N)? Y Enter the number: 78 78 is even Do you want to play again (Y

If you find these answers useful , please rate positive, thankyou

Add a comment
Know the answer?
Add Answer to:
Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate...
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
  • share your program enhance your work after submitting export to repl Due: Oct 12, 2020 05:00...

    share your program enhance your work after submitting export to repl Due: Oct 12, 2020 05:00 pm submit : back to classroom run Instructions from your teacher: #include <iostream> Complete this program. using namespace std; When the program is complete it should ask the user to enter a rectangle's length and width, then display the rectangle's area. The program calls the following functions which need to be completed: double getLength() { // Add code here } double getWidth() { //...

  • In the code (C++) below, if you input 2 as length and 4 as width, the...

    In the code (C++) below, if you input 2 as length and 4 as width, the output is: Enter the rectangle's length: 2 Enter the rectangle's width: 4 Length: 2 | Width: 4 | Area: 8 | Perimeter:12 We worked on this code during class, but there were some things that I did not understand. 1) how does compiler read"l" as length, "w" as width, etc.? I thought you had to update it first, like "w=width." 2) i thought you...

  • Write a program to display the area of a rectangle after accepting its length and width...

    Write a program to display the area of a rectangle after accepting its length and width from the user by means of the following functions: getLength – ask user to enter the length and return it as double getWidth – ask user to enter the width and return it as double getArea – pass width and length, compute area and return area displayArea – pass area and display it in this function. Expected Output: (i) Enter the length: 29 Enter...

  • Use DevC++ to implement a program uses objected oriented programming to calculate the area of a...

    Use DevC++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • Use C++ to implement a program uses objected oriented programming to calculate the area of a...

    Use C++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • Question 4 Write a program that display the area of a triangle. The program calls the...

    Question 4 Write a program that display the area of a triangle. The program calls the following two functions: .getBaseHeight - This function uses a reference parameter variables to accept a double arguments base and height. This function should ask the user to enter the triangle's buse and height. Input validation: base and height should be positive numbers. calArea - This function should accept the triangle's base and height as arguments and return the triangle's area. The area is calculated...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

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