Question

In Java, create a Square class that can be queried for its perimeter and area. Create...

In Java, create a Square class that can be queried for its perimeter and area. Create a second class , with a main(), to test the Square class. Write code in full detail.

Homework Answers

Answer #1

The text inside    // //    symbol are the comments *Leave this line*

import java.util.Scanner; //Scanner package used to take user input//
class Square
{
double a;   //varible declaration for side of square//
double are; //variable using to find area//
double peri; //variabe using to find perimeter//
  
   void method() //method to find area and perimeter//
   {
       System.out.println("The formula for the area of the square is : a^2");
       System.out.println("The formula for the perimeter of the square is : 4a");
       System.out.println("Enter side of the square: ");

       Scanner sc=new Scanner(System.in); //Scanner class for user input//
       a=sc.nextDouble();
       are=a*a;   //formula for area of square//
       peri=4*a; //formula for perimeter of square//
       System.out.println("Area of the Square: "+are+"cm/m/km"); //print statement for area///
       System.out.println("Perimeter of the Square: "+peri+"cm/m/km"); //print statement for perimeter//

   }
}
class Second
{
   public static void main(String[] args) { //main method//
       Square s=new Square(); //object of the Square class///
       s.method(); //calling method in Square class with object name//
   }
}

If you need any extra info, kindly comment me..

Know the answer?
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
Similar Questions
Please use javascript. Square Class Create a square class defined by the following Property side Methods...
Please use javascript. Square Class Create a square class defined by the following Property side Methods perimeter (side times 4) area (side squared) diagonal (square root of 2 * side squared) describe – shows the squares information as follows: Square with side 2 has perimeter of 8, area of 4, and diagonal of 2.828 Your program is to create three squares and use the describe method to show each squares information.
Write a parent class Rectangle in Java with an area calculation method in dynamic binding and...
Write a parent class Rectangle in Java with an area calculation method in dynamic binding and another area calculation method in static binding. Write a child class Square with an area calculation method that overrides the Rectangle class’s area method. ( in Java use static or final for static binding.) Write a main program that calls both the dynamically bound method and the statically bound method a large number of times (say 1000 times), timing the calls to static binding...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
(Please use java to write these questions) Q1. Create a class on an object Computer with...
(Please use java to write these questions) Q1. Create a class on an object Computer with two fields and two methods as follows: (5 points) field: cpu and memory, method: compile( ) and execute( ) Q2. Write a source code to activate a class created in Q1. (5 points)
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
JAVA Rational Numbers Create a Rational number class in the same style as the Complex number...
JAVA Rational Numbers Create a Rational number class in the same style as the Complex number class created in class. That is, implement the following methods: constructor add sub mul div toString You must also provide a Main class and main method to fully test your Rational number class.
Java Question 1. Create a POJO Class (Employee) with these properties: i. ID ii. Age iii....
Java Question 1. Create a POJO Class (Employee) with these properties: i. ID ii. Age iii. Name 2. Write the class to read into the memory. (Reader to read into the memory). 3. Write a Junit test for the Employee class.
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT