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
Why are there few effective HCI standards?
Oliga [24]

Answer:

<em>There are few effective Human Computer Interaction (HCI) standards because for one, standards are more suitable for hardware than software because they are relatively unstable. ... Some software product standards have been in place long before any formal standard documents were published.</em>

3 0
3 years ago
What does the plasma membrane do
aniked [119]

<u>Answer:</u>

The plasma membrane encloses specific structures.

<u>Explanation: </u>

plasma membrane is also called as cell membrane. Plasma membrane only  allows the particle to get in and pass out of the cell by osmosis and diffusion method from the outside environment. It is responsible for the molecular traffic inside the cell.

It helps in maintaining the shape of the cell. It has many proteins in it. Therefore cell membrane are responsible for having specific structures.

7 0
3 years ago
Diffusion of Ammonia in an Aqueous Solution Ammonia (A)-water (B) solution ta 278 K and 4 mm thick is in contact with an organic
Tom [10]

Answer:

Explanation:

The pictures below shows the whole explanation for the problem

4 0
4 years ago
HELP FAST WILL MARK BRAINLIEST (for a real answer)
Nata [24]

Answer:

B. 180 million joules

Explanation:

Apply the formula for heat transfer given as;

Q=m*c*Δt  where

Q = electrical energy consumed by the heater in joules

m= mass of air in the chamber in kg

c= specific heat of air in joules per kg degrees Celsius

Δt= change in temperatures in degrees Celsius

Given in the question;

m= 1200 kg

c= 1000 J/°C /kg

Δt = 180°-30°= 150° C

Substitute values in the equation to get Q as;

Q=m*c*Δt

Q= 1200 * 1000* 150

Q= 180000000 joules

Q = 180 million joules

<u>The correct answer option is B : 180 million joules.</u>

7 0
3 years ago
Select the correct answer. Felix aspires to be an engineer working for the government. What credentials will Felix require to ap
Aneli [31]

Answer:

OB

Explanation:

5 0
3 years ago
Other questions:
  • An FCC iron–carbon alloy initially containing 0.35 wt% C is exposed to an oxygen-rich and virtually carbon-free atmosphere at 14
    11·1 answer
  • The in situ moist unit weight of a soil is 17.3 kN/m3 and the moisture content is 16%. The specific gravity of soil solids is 2.
    12·1 answer
  • Assume you have a beam that is 15 feet long and carries a concentrated load of 900 pounds at a distance of 4 feet from R . React
    15·1 answer
  • Fin efficiency is defined in relation to the maximum possible heat transfer rate from the fin for given convective conditions. T
    10·1 answer
  • Kim is assigned to write an important company report with Philip and Tara, her coworkers. Because Kim prefers to work alone, she
    13·1 answer
  • When all network cables connect to one central point the network topology is typically referred to as a(n)_______
    6·1 answer
  • A hydraulic jump is formed in a 4m wide outlet just downstream of a control gate, which is located at the upstream end of the ou
    14·1 answer
  • When you receive a vertical promotion, you are more likely to _______ than when you receive a horizontal promotion. a. Deal with
    9·1 answer
  • Comparison of copper and aluminium conductors looking at their properties
    15·1 answer
  • Internal service quality means that an entire organization,
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!