Question

In: Computer Science

Question 3: Write the following Java program. A. Create a class called Quadrilateral that contains the...

Question 3: Write the following Java program.

A. Create a class called Quadrilateral that contains the following: [4 Marks]

• Five double type data fields for: side1, side2, side3, side4, and perimeter

• A no-argument constructor that creates a Quadrilateral with all sides equal to 1

• Methods named setSide1(double s1), setSide2(double s2), setSide3(double s3), and

setSide4(double s4) to set the values of the four sides

• A method named calculatePerimeter( ) to calculate and return the perimeter of the

quadrilateral

B. Create a child class called Square that contains a method named calculateArea() to

calculate the area of the Square. [2 Marks]

C. Create a java main class named TestQuad to do the following: [4 Marks]

• Create a Quadrilateral object named Quad1 and a Square object named Sq1.

• Call the methods setSide1(double s1), setSide2(double s2), setSide3(double s3), and

setSide4(double s4) to:

o set the sides of Quad1 to 10, 20, 25, and 30, respectively

o set all sides of Sq1 to 3

• Display the perimeter of Quad1 by calling the method calculatePerimeter ( ). Display

the perimeter of Sq1 by calling the method calculatePerimeter ( ).

• Display the area of Sq1 by calling the method calculateArea()

Hint: Perimeter = sum of all sides & Area of Square = side * side

Solutions

Expert Solution

Java code pasted below.

//class Quadrilateral
class Quadrilateral{
double side1,side2,side3,side4,perimeter;
//no-argument constructor
Quadrilateral()
{
this.side1=1;
this.side2=1;
this.side3=1;
this.side4=1;
}
//methods to set values for sides
void setSide1(double side1)
{
this.side1=side1;
}
void setSide2(double side2)
{
this.side2=side2;
}
void setSide3(double side3)
{
this.side3=side3;
}
void setSide4(double side4)
{
this.side4=side4;
}
double calculatePerimeter()
{
return side1+side2+side3+side4;
}
}
//class Square
class Square extends Quadrilateral{
double calculateArea()
{
return side1*side1;
}
}
//Main class
class TestQuad{
public static void main(String args[])
{
//creating the object of quadrilateral
Quadrilateral Quad1=new Quadrilateral();
//creating the object of square
Square Sq1=new Square();
//set the sides of Quadrilateral
Quad1.setSide1(10);
Quad1.setSide2(20);
Quad1.setSide3(25);
Quad1.setSide4(30);
//set the all sides of square to 0
Sq1.setSide1(3);
Sq1.setSide2(3);
Sq1.setSide3(3);
Sq1.setSide4(3);
//Display the perimeter of quadrilateral
double perimeter;
System.out.println("Perimeter of Quadrilateral="+Quad1.calculatePerimeter());
//Display the perimeter of square
System.out.println("Perimeter of Square="+Sq1.calculatePerimeter());
//calculate the area of square
double area;
System.out.println("Area of Square="+Sq1.calculateArea());

}
}

Output Screen


Related Solutions

Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final grade for the course. Assume the following (you can hard-code these values in your program). Assignments are worth 30% Labs are worth 40% Mid-term is worth 15% Final is worth 15% Ask for the grades in each category. Store them in a double variable. Print out the final percentage grade.           For example, Final Grade Calculation Enter percentage grades in the following order. Assignments,...
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the method main. The program should ask for information from the user and then print it. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java AskInfoPrintInfo enter your name: jon doe enter your address (first line): 23 infinite loop lane enter your address (second line): los angeles,...
UseMath Write a program called UseMath. It should contain a class called UseMath that contains the...
UseMath Write a program called UseMath. It should contain a class called UseMath that contains the method main. The program should ask for a number from the user and then print the information shown in the examples below. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java UseMath enter a number: 0 the square root of 0.0 is: 0.0 rounded to the...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT