Question

Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your

plz write if it is in another class or package
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

I developed 2 classes whhich extends abstract class and two class which implements interface.

Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// GeometricFigure.java

public interface GeometricFigure {
   double getArea();
  
}

================================

// Figure.java

public abstract class Figure {
private double height;
private double width;
  
/**
* @param height
* @param width
*/
public Figure(double height,double width) {
this.height = height;
this.width = width;
}
  
/**
* @return the height
*/
public double getHeight() {
return height;
}

/**
* @param height the height to set
*/
public void setHeight(double height) {
this.height = height;
}

/**
* @return the width
*/
public double getWidth() {
return width;
}

/**
* @param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
  
public abstract double calcPerimeter();
  
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "with height = " + height + " and with width = " + width;
}


}

==================================

// Rectangle.java

public class Rectangle extends Figure {

   public Rectangle(double height, double width) {
       super(height, width);
   }

   @Override
   public double calcPerimeter() {
       return 2 * (getWidth() + getHeight());
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Rectangle with "+super.toString();
   }

  
}


=====================================

// Triangle.java

public class Triangle extends Figure implements GeometricFigure {
  
   public Triangle(double height, double width) {
       super(height, width);
   }

   @Override
   public double getArea() {
       double area=0.5*getWidth()*getHeight();
       return area;
   }

   @Override
   public double calcPerimeter() {
       double hyp=Math.sqrt(getHeight()*getHeight()+getWidth()*getWidth());
       double perimeter=getHeight()+getWidth()+hyp;
       return perimeter;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Triangle with "+super.toString();
   }
  

}

===================================

// Circle.java

public class Circle implements GeometricFigure {

   private double radius;

   /**
   * @param radius
   */
   public Circle(double radius) {
       super();
       this.radius = radius;
   }

   /**
   * @return the radius
   */
   public double getRadius() {
       return radius;
   }

   /**
   * @param radius
   * the radius to set
   */
   public void setRadius(double radius) {
       this.radius = radius;
   }

   @Override
   public double getArea() {
       double area = Math.PI * radius * radius;
       return area;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Circle With Radius :"+radius;
   }
  

}

====================================

// Test.java

public class Test {

   public static void main(String[] args) {
  
       Figure gf[]={new Rectangle(4, 5),new Triangle(10, 4),new Rectangle(6, 8),new Triangle(6,4)};
       for(int i=0;i<gf.length;i++)
       {
           System.out.println(gf[i]+" Its Perimeter :"+gf[i].calcPerimeter());
           System.out.println("-------------------------------------------");
       }

      
       GeometricFigure f[]={new Triangle(10, 6),new Circle(3.5),new Triangle(8, 4),new Circle(5.5)};
       for(int i=0;i<gf.length;i++)
       {
           System.out.println(f[i]+" Its Area :"+f[i].getArea());
           System.out.println("-------------------------------------------");
       }
      
   }

}

===================================

output:

Rectangle with with height = 4.0 and with width = 5.0 Its Perimeter :18.0
-------------------------------------------
Triangle with with height = 10.0 and with width = 4.0 Its Perimeter :24.77032961426901
-------------------------------------------
Rectangle with with height = 6.0 and with width = 8.0 Its Perimeter :28.0
-------------------------------------------
Triangle with with height = 6.0 and with width = 4.0 Its Perimeter :17.21110255092798
-------------------------------------------
Triangle with with height = 10.0 and with width = 6.0 Its Area :30.0
-------------------------------------------
Circle With Radius :3.5 Its Area :38.48451000647496
-------------------------------------------
Triangle with with height = 8.0 and with width = 4.0 Its Area :16.0
-------------------------------------------
Circle With Radius :5.5 Its Area :95.03317777109123
-------------------------------------------

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
plz write if it is in another class or package Question 1: 1. Create a new...
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
  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace....

    Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace. You will add the java files after you have completed them in Part 1 and Part 2 to this location. Part 1: Abstract classes From class, an abstract class represents some generic concept. Subclasses then provide their own specific implementations of any abstract methods of the abstract class. 1. Implement an abstract class called Shape it was discussed in the lecture. Include an abstract...

  • In JAVA: For this question, you need to create two interface programs in a package and...

    In JAVA: For this question, you need to create two interface programs in a package and import them to the class where you implement the methods. You can create a new package in your assignment4 project (or package) to contain the two interface programs. One of the interface program has two methods ( countOne(), countTwo() ) and the other interface program has only one method (countThree ( ) ). In a separate package, write a class to implement the two...

  • 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the...

    1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – : For this question submit a UML as the answer. Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of Electric cars and two for Altfuel cars( for...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • 1.     When a sub class object is created, when is the call to the super class...

    1.     When a sub class object is created, when is the call to the super class constructor made? How does a programmer call the super class constructor from the sub class? What do all classes indirectly extend? What methods does every class inherit from the Object class? 2.     When writing methods in a sub class, how can those methods call the methods from the parent class? 3.     Which class is more specific, a super class or a sub class? 4.    ...

  • object oriented programming java homework question about abstracting and interfaces 2. Definition of a hierarchy of...

    object oriented programming java homework question about abstracting and interfaces 2. Definition of a hierarchy of fruits is given below. • Fruit contains an abstract method getVitamin() that returns String. Fruit contains a String field color. Fruits are Apple, Banana, Strawberry and Blackberry. Apples are green, bananas are yellow, strawberries are red, blackberries are black. All these classes have zero parameter constructors. O Apple's vitamins are "A B12". 0 Banana's vitamins are "CD". O Strawberry's vitamins are “B5 E". 0...

  • java. Question 3: The payment transaction scenario could be represented by the following hierarchy: Payments VISA...

    java. Question 3: The payment transaction scenario could be represented by the following hierarchy: Payments VISA MASTERCARD Paypal Create Payments as a superclass and VISA, MASTERCARD, PAYPAL as subclasses Create an interface PaymentsInterface with one method called payment Info. Create classes VISA, MASTERCARD, PAYPAL that implement Payments For each of the three classes (VISA, MASTERCARD, PAYPAL), create one constructor Constructors are used to create objects with initial balance with US dollars (USD). The interface class method "paymentinfo" should be overridden...

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