Pls screenshot program and check output //Thanls alot __________________________
ID: 3825623 • Letter: P
Question
Pls screenshot program and check output //Thanls alot
___________________________________________________________
Q1: Implement the following "Has-a" relationship. Circle radius double 1.0 color:String red +Circle() +Circle(radius double) +get Radius(): double +getArea(): double +toString ():String superclass subclass cylinder height double 1.0 +cylinder +cylinder (radius:double) +cylinder (radius double, height:double) +getHeight(): double double Q 2: Write an AWT GUI application called AWTAccumulator, which has four components: 1. a java awt Label "Enter an integer and press enter 2. an input java.awt TextField: 3. a java awt Label "The accumulated sum is", and 4. a protected (read-only) ava awt TextField for displaying the accumulated sum. Frame e AWT Accumulator Enter an integer al 123 Accumulated su Text Field Label Text Field (Component) (Component) (Component) Source of ActionEvent non-editableExplanation / Answer
// Q1. public class Circle { public double radius = 1.0; public String color = "red"; public Circle() { } public Circle( double radius ) { this.radius = radius; } public double getRadius() { return radius; } public double getArea() { return Math.PI * radius * radius; } @Override public String toString() { return "Circle{" + "radius=" + radius + ", color='" + color + ''' + '}'; } } public class Cylinder extends Circle { double height = 1.0; public Cylinder() { } public Cylinder( double height ) { this.height = height; } public Cylinder( double radius, double height ) { super(radius); this.height = height; } public double getHeight() { return height; } public double getVolume() { return getArea() * height; } } // Q2. import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class AWTAccumulator extends Frame { AWTAccumulator() { setLayout(new FlowLayout()); Label label = new Label("Enter an integer"); add(label); TextField field = new TextField(10); add(field); Label label2 = new Label("Accumulated sum is:"); add(label2); TextField field2 = new TextField(10); add(field2); field2.setEditable(false); addWindowListener(new WindowAdapter() { public void windowClosing( WindowEvent e ) { dispose(); } }); field.addKeyListener(new KeyAdapter() { @Override public void keyPressed( KeyEvent e ) { if( e.getKeyChar() == ' ' ) { String input = field.getText(); String input2 = field2.getText(); try { int num = Integer.parseInt(input); int result; if( input2.isEmpty() ) result = 0; else result = Integer.parseInt(input2); result += num; field2.setText(result + ""); field.setText(""); } catch( NumberFormatException nfe ) { } } } }); } public static void main( String[] args ) { Frame frame = new AWTAccumulator(); frame.setSize(300, 100); frame.setVisible(true); } }