1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nonamiya [84]
4 years ago
6

You will build four classes that inherit from an existing Shape superclass. These subclasses should all have the phrase "extends

Shape" in their class signatures, as in: "public class MyNewShape extends Shape {". Your classes can and should be tested in isolation before combining your classes with the final drivers, so consider building a main in each subclass that tests out just that class. When you are confident in your subclasses, you can use this driver to see your shapes render to a JPanel. Note that this is also one of the drivers I will use when grading your work, so you should be sure to verify that these classes compile and execute before submitting.
Engineering
1 answer:
alexgriva [62]4 years ago
8 0

Answer:

Java program is explained below

Explanation:

Implemented the Circle and Square subclasses. And modified the PolyDemo class to use them. Use the following files along with Shape and Spray classes.

To indent code in eclipse, select code and press Ctrl+a and then Ctrl+i.

Please don't forget to rate the answer if it helped. Post a comment in case of any issues.

PolyDemo.java( modified)

import java.util.*;

import javax.swing.*;

import java.awt.*;

/*

* Class PolyDemo (is a JFrame) and PolyDemoPanel (is a JPanel)

*

* Author: Rob Nash

*/

class PolyDemo extends JFrame {

public PolyDemo() {

getContentPane().add( new PolyDemoPanel() );

//just some windowing stuff that must happen for all Frames

setSize( 300,300 );

setVisible( true );

setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}

public static void main( String args[] ) {

PolyDemo myApp = new PolyDemo();

}

//this is our first "inner" or internal class

//the purpose of this class is solely to support the JFrame class above, and I don't want it reused in arbitrary contexts, so by nesting this class here

//I can indicate the intent a bit more clearly that this class "goes with" the class above it

//In general, each class is a separate entity that should be contained in a separate file

public class PolyDemoPanel extends JPanel {

Shape[] myShapes= new Shape[20];

public PolyDemoPanel() {

//Shape a = new Shape( getRandInt(), getRandInt());

//Shape b = new Circle( getRandInt(), getRandInt(), getRandInt() );

//a = new Square(getRandInt(), getRandInt(), getRandInt(), getRandInt() );

//a = getRandShape();

//( (Circle) b ).getRadius();

/*********************************************************************************************************************

* Code for populating our myShapes changes minimally when new classes are introduced (only in getRandShape())

*********************************************************************************************************************/

for( int i = 0; i < 20; i++ ) {

myShapes[i] = getRandShape();

}

}

/*********************************************************************************************************************

* Code for drawing our shapes doesn't change at all! Since we intended to take advantage of polymorphism, we coded

* this "in general" with respect to the superclass, and not specific to any subclass.

*********************************************************************************************************************/

public void paint( Graphics g ) {

super.paint(g); //don't remove - required for GUI widgets to draw correctly

/************************

* Late Binding Demo

************************/

for( int i = 0; i < myShapes.length; i++ ){

//which draw method is invoked here? There are many forms of the method (polymorphic), so which is chosen?

//Java has RTTI about every object, and it uses this info to choose the correct method to invoke!

myShapes[i].draw( g );

}

}

public int getRandInt() {

return ( (int) ( Math.random() * 200 ) );

}

public Shape getRandShape() {

Shape retVal = null;

final int x = getRandInt();

final int y = getRandInt();

/********************************

* Polymorphic extensibility demo

*

*******************************/

switch( ( int )(Math.random() * 4) ) {

case 0: retVal = new Square( x, y, getRandInt(), getRandInt());

break;

case 1: retVal = new Spray( x,y );//Cube( x, y, getRandInt(), getRandInt(), getRandInt() );

break;

case 2: retVal = new Spray( x,y );

break;

case 3: retVal = new Circle( x,y,getRandInt() );////new Cylinder( x,y, getRandInt(), getRandInt() );

break;

}

return retVal;

}

}

}

Circle.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

public class Circle extends Shape {

private int radius;

public Circle(int x, int y, int radius)

{

super(x,y);

this.radius = radius;

}

double getRadius()

{

return radius;

}

void setRadius(int rad)

{

this.radius = rad;

}

@Override

public double getArea() {

return Math.PI * radius * radius;

}

@Override

public void draw(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g.setColor(Color.red);

g2d.drawOval(getX(), getY(), radius, radius);

}

}

Square.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

public class Square extends Shape {

private int width;

private int height;

public Square(int x, int y, int width, int height) {

super(x,y);

this.width = width;

this.height = height;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

@Override

public double getArea() {

return width * height;

}

@Override

public void draw(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g.setColor(Color.blue);

g2d.drawRect(getX(), getY(), width, height);

}

}

You might be interested in
For the following circuit diagram, if A=010 , B= 101.
Fantom [35]

Answer:

cgghhhh chick jjkkkkkki

4 0
3 years ago
A 3-m-high, 11-m-wide rectangular gate is hinged at the top edge at A and is restrained by a fixed ridge at B. Determine the hyd
serg [7]

Answer:

its a

Explanation:

its a

5 0
4 years ago
Side milling cutter is an example of ______ milling cutter.
dusya [7]

Answer:

special type

Explanation:

As per the classification of milling cutters. This cutter can handle deep and long open slots in a more comfortable manner, which increase the productivity.

6 0
3 years ago
Reduce the force F ij = + (2 5 ) kN to point A(2m,3m) that acts on point B( 3m,5m) - .
Alexxx [7]

Given :

Force, \vec{F}= (2\hat{i} + 5\hat{j})\ kN.

Force is acting at point A( 2 m, 3 m ) and B( 3 m, 5 m )

To Find :

The work done by force F .

Solution :

Displacement vector between point A and B is :

\vec{d} = (3-2)\hat{i} + (5-3)\hat{j}\\\\\vec{d} = \hat{i} + 2\hat{j}

Now, we know work done is given by :

W = \vec{F}.\vec{d}\\\\W= (2\hat{i} + 5\hat{j}).(\hat{i}+\hat{2j})\\\\W = (2\times 1) +( 5\times 2) \ kJ\\\\W = 12 \ kJ

W = 12000 J

Therefore, work done by force is 12000 J .

6 0
3 years ago
Define extensive and intensive properties of thermodynamic system.
Flura [38]

Answer:

An intense property is a physical attribute of a system that is independent of the size of the system or the quantity of material it contains. An extensive property of a system, on the other hand, is dependent on the size of the system or the amount of material in it.

Explanation:

7 0
3 years ago
Read 2 more answers
Other questions:
  • A turbine produces shaft power from a gas that enters the turbine with a (static) temperature of 628 K, a velocity of 143 m/s an
    7·1 answer
  • The underground storage of a gas station has leaked gasoline into the ground. Among the constituents of gasoline are benzene, wi
    12·2 answers
  • Help me! Phone Phoebe on 07375410044.
    7·2 answers
  • Single point cutting tool removes material from a rotating work piece to generate a cylinder is called • Facing Tuming • Both 1
    6·1 answer
  • Air enters the compressor of a cold air-standard Brayton cycle with regeneration and reheat at 100 kPa, 300 K, with a mass flow
    10·1 answer
  • 6
    5·1 answer
  • Piping layout carrying liquid water at 70°F at a volumetric flow rate of 0.2 is composed of four sections of 4-in. Diameter stee
    12·1 answer
  • 1<br>M<br>A BLIND COOK WHO DEFEATED<br>OVER 30,000 HOME COOKS!<br>y of​
    13·1 answer
  • How could increasing the budget for testing have prevented the problem experienced by the mars orbiter?
    7·1 answer
  • One of the best ways to increase engine power and control detonation and preignition is to?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!