Question

Using the IST Linux system create the following Java command line inheritance application Lab4. Create the following project Java files: Point.java, Shape.java, Circle.java, Triangle.java, Rectangle.java, and Lab4.java

Point.java will contain two coordinates x and y. This will be reused to create point objects to draw all the shapes.

Shape.java will be the parent class and will contain a Point type.

Circle.java, Triangle.java, Rectangle.java will all be children of Shapes.java class

Each class (Circle, Triangle, Rectangle) will contain the private class point type variables needed to draw the shape.

Each class (Circle, Triangle, Rectangle) will contain two constructor methods

constructor no input arguments but will set (hardcoded) the private point variables needed to draw the shape. It will also print out the shape type and the points set for that shape.

constructor the number of point type arguments needed to set the private point variables needed to draw the shape.  It will also print out the shape type and the points set for that shape.

Lab4.java will be the starting point for running the program

Lab4.java will create three objects one from the Circle class, one from Triangle class, and one from Rectangle class that calls the no input argument constructor 1 containing hardcoded points

Lab4.java will create another three objects one from the Circle class, one from Triangle class, and one from Rectangle class that will call constructor 2 and pass in the point type variables needed to draw the shape.

public class Lab41 public static void main (String[] args){ Circle c1 - new Circle(); Point p1 - new Point(4, 5); Circle c22 = new Circle(p1);

Screen Shot 2017-02-16 at 9.53.19 PM.pngScreen Shot 2017-02-16 at 9.54.54 PM.png

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Point.java : Point class with x-axis and y-axis value as attribute

public class Point {

int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

// Shape.java : Parent Class for all shapes contining one Point attribute

public class Shape {

protected Point point;

}

// Circle.java : Circle class is child of Shape class containing additional radius attribute

public class Circle extends Shape {

private int radius;

public Circle() {
point = new Point(2,3);
radius = 2;
System.out.println("Circle Point: X:"+point.x+ " Y:"+point.y);
System.out.println("Circle radius:"+radius);
}

public Circle(Point point) {
super();
this.point = point;
radius = 5;
System.out.println("Circle Point: X:"+point.x+ " Y:"+point.y);
System.out.println("Circle radius:"+radius);
}
}

//Triangle.java : Triangle is child of Shape class containing additional two points to draw

public class Triangle extends Shape {

private Point point2;
private Point point3;

public Triangle() {
point = new Point(1,1);
point2 = new Point(2,2);
point3 = new Point(1,3);

System.out.println("Triangle Point-1: X:"+point.x+ " Y:"+point.y);
System.out.println("Triangle Point-2: X:"+point2.x+ " Y:"+point2.y);
System.out.println("Triangle Point-3: X:"+point3.x+ " Y:"+point3.y);
}

public Triangle(Point point1, Point point2, Point point3) {
point = point1;
this.point2 = point2;
this.point3 = point3;

System.out.println("Triangle Point-1: X:"+point.x+ " Y:"+point.y);
System.out.println("Triangle Point-2: X:"+point2.x+ " Y:"+point2.y);
System.out.println("Triangle Point-3: X:"+point3.x+ " Y:"+point3.y);
}
}

//Rectangle.java : Rectangle is child of Shape class containing additional 3 points to draw the shape

public class Rectangle extends Shape {

private Point point2;
private Point point3;
private Point point4;

public Rectangle() {
point = new Point(1,1);
point2 = new Point(1,2);
point3 = new Point(2,1);
point4 = new Point(2,2);
System.out.println("Rectangle Point-1: X:"+point.x+ " Y:"+point.y);
System.out.println("Rectangle Point-2: X:"+point2.x+ " Y:"+point2.y);
System.out.println("Rectangle Point-3: X:"+point3.x+ " Y:"+point3.y);
System.out.println("Rectangle Point-4: X:"+point4.x+ " Y:"+point4.y);
}

public Rectangle(Point point1, Point point2, Point point3, Point point4) {
super();
point = point1;
this.point2 = point2;
this.point3 = point3;
this.point4 = point4;

System.out.println("Rectangle Point-1: X:"+point.x+ " Y:"+point.y);
System.out.println("Rectangle Point-2: X:"+point2.x+ " Y:"+point2.y);
System.out.println("Rectangle Point-3: X:"+point3.x+ " Y:"+point3.y);
System.out.println("Rectangle Point-4: X:"+point4.x+ " Y:"+point4.y);
}

}

//Lab4.java : Lab4 is execution point of the complete assignment

public class Lab4 {

public static void main(String[] args) {
Circle c1 = new Circle();
Point p1 = new Point(4, 5);
Circle c22 = new Circle(p1);

Triangle t1 = new Triangle();
Point p2 = new Point(1,1);
Point p3 = new Point(1,7);
Triangle t2 = new Triangle(p1, p2, p3);

Rectangle r1 = new Rectangle();
Point p4 = new Point(9,5);
Rectangle r2 = new Rectangle(p1, p2, p3, p4);

}
}

// This application teaches us one of the Object oriented concepts called as "Inheritance".

// In this we separate out the common property in a parent class and all child class inherit it from parent.

// We use extends keyword to inherit propety of another class.

// Instructions for execution :

// Place all classes in different .java files. file names are mentioned in comment in bold.

//on command line we can refer all java files by specifying *.java in respected folder.

// for compilation execute >javac *.java

// to run the assignment execute >java Lab4

// It should give following output

// Circle Point: X:2 Y:3

// Circle radius:2

// Circle Point: X:4 Y:5

// Circle radius:5

// Triangle Point-1: X:1 Y:1

// Triangle Point-2: X:2 Y:2

// Triangle Point-3: X:1 Y:3

// Triangle Point-1: X:4 Y:5

// Triangle Point-2: X:1 Y:1

// Triangle Point-3: X:1 Y:7

// Rectangle Point-1: X:1 Y:1

// Rectangle Point-2: X:1 Y:2

// Rectangle Point-3: X:2 Y:1

// Rectangle Point-4: X:2 Y:2

// Rectangle Point-1: X:4 Y:5

// Rectangle Point-2: X:1 Y:1

// Rectangle Point-3: X:1 Y:7

// Rectangle Point-4: X:9 Y:5

Add a comment
Know the answer?
Add Answer to:
Using the IST Linux system create the following Java command line inheritance application Lab4. Create the...
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
  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance...

    Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance structure of various shapes. Details: Implement the following Shape hierarchy: a box around each shape name and arrows pointing to the shape from each (Circle, Square, Triangle) Shape Circle Square Triangle    Use the following as a guide: The Shape class should be abstract and contain two abstract methods: getArea – Which calculates the area of the shape and returns that value as a...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Please write in Java Language Write an abstract class Shape with an attribute for the name...

    Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...

  • ****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...

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

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