I need help to finalize one of my assignments! I have finished all the code exce
ID: 3563040 • Letter: I
Question
I need help to finalize one of my assignments! I have finished all the code except a "for loop" to create a 'hollow' or empty isosceles triangle to print output from my interface program in my IsoscelesRightTriangle class. I have figured out the rectangle and square class just fine, I just cannot finalize the right triangle (isosceles) one (and it has to be hollow).
Please see my code below ---can someone quickly help me to find a way to code the method (print()). This will be printed in my InterfaceApp class ---but I cannot figure out how to make it work? Can someone please help me to code the 'for loop' on this? I would very much appreciate any help on this? The triangle has a height of 6, and a base of 6. It is a right triangle. I need it to print with asterisk around the perimeter. Output shown below:
I will also include my InterfaceApp code to show how it will be printed from that class! Please help me to make it print out correctly!!!
IsoscelesRightTriangle (6)
Perimeter: 20.5
Area: 18.0
*
* *
* *
* *
* *
* * * * * *
package interfaceApp;
public class IsoscelesRightTriangle implements Shape, Printable
{
int h;
double perimeter;
double area;
public IsoscelesRightTriangle(int he)
{
h= he;
}
public double perimeter()
{
return h + h + Math.sqrt((6*6)+(6*6));
}
public double area()
{
return (double)(h*h)/2;
}
public void print()
{
int i, j, k;
// loop for the number of lines
for (i = 1; i <= 6; i++)
{
for (k=1; k<= h; k++){
System.out.println("*");
}
}
}
public String toString()
{
return "IsoscelesRightTriangle(6)";
}
}
________
package interfaceApp;
public class InterfaceApp
{
public static void main(String[] args)
{
Rectangle myRectangle = new Rectangle(6, 3);
Square mySquare = new Square(5);
IsoscelesRightTriangle myTriangle = new IsoscelesRightTriangle(6);
System.out.println(myRectangle);
System.out.println();
System.out.println(mySquare);
System.out.println();
System.out.println(myTriangle);
System.out.println();
Circle myCircle = new Circle(5);
System.out.println(myCircle);
System.out.println("Diameter: " + myCircle.diameter());
System.out.printf("Circumference: %.1f " , myCircle.perimeter());
System.out.println();
Shape[] array = new Shape[]{mySquare,myRectangle,myTriangle,myCircle};
System.out.println("Shape Array:");
System.out.println("-----------");
for (Shape S : array)
{
System.out.println(S);
if (S instanceof Square)
{
((Square) S).perimeter();
((Square) S).area();
System.out.printf("Perimeter: %.1f ", mySquare.perimeter());
System.out.println("Area: " + mySquare.area());
((Square) S).print();
System.out.println();
}
if (S instanceof Rectangle)
{
((Rectangle) S).perimeter();
((Rectangle) S).area();
System.out.printf("Perimeter: %.1f ", myRectangle.perimeter());
System.out.println("Area: " + myRectangle.area());
((Rectangle) S).print();
System.out.println();
}
if (S instanceof IsoscelesRightTriangle)
{
((IsoscelesRightTriangle) S).perimeter();
((IsoscelesRightTriangle) S).area();
System.out.printf("Perimeter: %.1f ", myTriangle.perimeter());
System.out.println("Area: " + myTriangle.area());
((IsoscelesRightTriangle) S).print();
System.out.println();
}
if (S instanceof Circle)
{
((Circle) S).perimeter();
((Circle) S).area();
System.out.printf("Circumference: %.1f ", myCircle.perimeter());
System.out.println("Area: " + myCircle.area());
}
}
}
}
IsoscelesRightTriangle (6)
Perimeter: 20.5
Area: 18.0
*
* *
* *
* *
* *
* * * * * *
Explanation / Answer
import java.util.Scanner; public class MyShapes { public static Scanner scan = new Scanner(System.in); public static int Menu() { System.out.println(" Select a Shape or Exit: "); System.out.println("1. Square"); System.out.println("2. Circle"); System.out.println("3. Triangle"); System.out.println("4. Exit"); System.out.println(" Enter choice:"); int option = scan.nextInt(); return option; }// end menu public static void main(String[] args) { int option = 0; while (option != 4) { option = Menu(); Shape shape = null; switch(option) { case 1: shape = new Circle(scan.nextDouble()); break; case 2: Shape shape = new Square(scan.nextDouble()); break; case 3: shape = new Triangle(scan.nextDouble()); break; case 4: System.out.println("System closing"); System.out.println("----------- "); //System.exit(0);Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.