Advance programmers, please be advised, this class is an Introduction to compute
ID: 3646232 • Letter: A
Question
Advance programmers, please be advised, this class is an Introduction to computer languages. Just a basic course. Will probably not make much sense in real world situations. Please keep that in mind. Thanks***************************************************************************
Create a Java program based on the geometric shapes example described at the beginning of this
lesson using Classes Square, Triangle, Rectangle, and Circle to help describe inheritance and
polymorphism using the circumference() method. The program will calculate the circumference of a
designated shape.
Make sure you create the program in a loop and have the program prompt you for another shape and
calculation. Enter 5 for exit.
***NOTE***
You should first create a Square superclass, and then from it create Rectangle, Circle, and
Triangle subclasses. In the Square class create a circumference() method (e.g., circumf()) and then
re-define that method in each subclass to calculate the area of the appropriate shape.
Also, you should now be using the JOptionPane.showMessageDialog() method (or something similar) for
displaying your output instead of System.out.println().
System.out.println().
The program should begin by prompting you for the shape you want to calculate the circumference
for
Explanation / Answer
import java.io.*; import javax.swing.JOptionPane; public class Square { int s; Square(int s) { this.s=s; } int circumf() { return 4*s; } } public class Triangle extends Square { int side; Triangle(int side) { this.side=side; } int circumf() { return 3*side; } } public class Rectangle extends Square{ int length,breadth; Rectangle(int length,int breadth) { this.length=length; this.breadth=breadth; } int circumf() { return 2*(length+breadth); } } public class Circle extends Square { int r; Circle(int r) { this.r=r; } int circumf() { return 2*3.14*r; } } public class Test { public static void main(String args[]) { String choice,temp; int p,s; do{ choice = JOptionPane.showInputDialog ( "Enter 1-square, 2- rectangle, 3-circle, 4-triangle, and 5-Exit program. " ); p=Integer.parseInt(choice); switch(p) { case 1:JOptionPane.showMessageDialog(null,"you selected square"); temp=JOptionPane.showInputDialog ( "enter side length"); s=Integer.parseInt(temp); Square sq=new Square(s); JOptionPane.showMessageDialog(null, sq.circumf()); break; case 2:JOptionPane.showMessageDialog(null,"you selected rectangle"); temp=JOptionPane.showInputDialog ( "enter length"); String temp1=JOptionPane.showInputDialog ( "enter breadth"); s=Integer.parseInt(temp); int s1=Integer.parseInt(temp1); Rectangle rec=new Rectangle(s,s1); JOptionPane.showMessageDialog(null, rec.circumf()); break; case 3:JOptionPane.showMessageDialog(null,"you selected circle"); temp=JOptionPane.showInputDialog ( "enter radius"); s=Integer.parseInt(temp); Circle c=new Circle(s); JOptionPane.showMessageDialog(null, c.circumf()); break; case 4:JOptionPane.showMessageDialog(null,"you selected Triangle"); temp=JOptionPane.showInputDialog ( "enter side length"); s=Integer.parseInt(temp); Triangle sq=new Triangle(s); JOptionPane.showMessageDialog(null, sq.circumf()); break; } }while(!p.equals(5)); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.