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 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...
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 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 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 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.
This should be a simple code for beginning Java; Create a Student class with four instance...
This should be a simple code for beginning Java; Create a Student class with four instance variables, a default constructor, a 4-arg constructor, and getters and setters for each instance variable.
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT