//Implement a subclass Square that extends the Rectangle class. In the constructor,
accept the x- and y-positions of the center and the side length of the square. Call the
setLocation and setSize methods of the Rectangle class. Look up these methods in the
documentation for the Rectangle class. Also supply a method getArea that computes
and returns the area of the square. Write a sample program that asks for the center
and side length, then prints out the square (using the toString method that you
inherit from Rectangle) and the area of the square.
//Ok... So this is last minute, but I don't understand what is wrong with my code it is giving me the error that square cannot be resolved to a type... So here is my Class:
import java.awt.Rectangle;
public class Squares22 extends Rectangle
{
public Squares22(int x, int y, int length) {
setLocation(x - length / 2, y - length / 2);
setSize(length, length);
}
public int getArea() {
return (int) (getWidth() * getHeight());
}
public String toString() {
int x = (int) getX();
int y = (int) getY();
int w = (int) getWidth();
int h = (int) getHeight();
return "Square[x=" + x + ",y=" + y + ",width=" + w + ",height=" + h
+ "]";
}
}
//And this is my tester class...
import java.util.Scanner;
public class Squares22Tester
{
public static void main(String[] args)
{
Scanner newScanx = new Scanner(System.in);
Scanner newScany = new Scanner(System.in);
Scanner newScanl = new Scanner(System.in);
System.out.println("Enter x:");
String x2 = newScanx.nextLine();
System.out.println("Enter y:");
String y2 = newScany.nextLine();
System.out.println("Enter length:");
String l2 = newScanl.nextLine();
int x = Integer.parseInt(x2);
int y = Integer.parseInt(y2);
int length = Integer.parseInt(l2);
Square sq = new Square(x, y, length);
System.out.println(sq.toString());
}
}
//Can anyone please help my assignment is due at midnight.. It says square cannot be resolved to a type on the tester class when compliling....
Square isn't the name of your class. The name of the class is 'Squares22'. This is why 'Square' cannot be recognized.
Change Square in the test to Squares22 or vice versa. This should solve your issues.
Related
How can I fix the multiplication error?
The formula is inside the nested class Area1 and I can't get the total value or product given by the user
I've tried giving it a value like:
side1 = 0;
or
side1 = 1;
The answer is always equal to 0
public void getArea(){
int area = side1 * side2;
System.out.println("Area of square: " + area);
Area1 square = new Area1(side1,side2);
square.getArea();
import java.util.Scanner;
public class Area1 {
int side1;
int side2;
int height;
int base;
int width;
int length;
Area1 (int side1,int side2,int height,int base, int width, int length){
this.side1 = side1;
this.side2 = side2;
this.width = width;
this.length = length;
}
private Area1(int height, int base) {
this.height = height;
this.base = base;
}
public void getArea(){
int area = side1 * side2;
System.out.println("Area of square: " + area);
}
public void getArea1(){
int area2 = height * base / 2;
System.out.println("Area of triangle: " + area2);
}
public void getArea2(){
int area2 = width * length;
System.out.println("Area of rectangle: " + area2);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int side1 = 0;
int side2 = 0;
int height;
int base;
int width;
int length;
System.out.println("-----FOR SQUARE-----");
System.out.println("Please enter 1st side of square: ");
side1 = input.nextInt();
System.out.println("Please enter 2nd side of square: ");
side2 = input.nextInt();
System.out.println("-----FOR TRIANGLE-----");
System.out.println("Please enter height of triangle: ");
height = input.nextInt();
System.out.println("Please enter base of triangle: ");
base = input.nextInt();
System.out.println("-----FOR RECTANGLE-----");
System.out.println("Please enter width of rectangle: ");
width = input.nextInt();
System.out.println("Please enter length of rectangle: ");
length = input.nextInt();
System.out.println("=====THE RESULTS ARE=====");
Area1 square = new Area1(side1,side2);
square.getArea();
Area1 triangle = new Area1(height, base);
triangle.getArea1();
Area1 rectangle = new Area1(width,length);
rectangle.getArea2();
}
}
The solution is simple:
You are setting only two properties (you are using single constructor only):
private Area1(int height, int base) {
this.height = height;
this.base = base;
}
So you are setting only two fields.
And in our compute methods you are using other fields, that are not set. And because they are primitive types, they are set to zero. Youn need to use other full arg constructor.
This is a great candidate for object oriented programming:
abstract class Figure {
abstract void getArea();
}
public class Square extends Figure {
private final int side1;
private final int side2;
Square(int side1, int side2) {
this.side1 = side1;
this.side2 = side2;
}
#Override
void getArea() {
int area = side1 * side2;
System.out.println("Area of square: " + area);
}
}
public class Triangle extends Figure {
private final int base;
private final int height;
Triangle(int base, int height) {
this.base = base;
this.height = height;
}
#Override
void getArea() {
int area2 = height * base / 2;
System.out.println("Area of triangle: " + area2);
}
}
And now you can compute:
System.out.println("=====THE RESULTS ARE=====");
Figure square = new Square(side1, side2);
square.getArea();
Figure triangle = new Triangle(height, base);
triangle.getArea();
With this approach you will not have to multiple variables if new figure will be added. You will not have to struggle with large constructor. Lastly: you are using only few fields for each figure, rest is useless.
The issue is that, in the constructor you assign the values only for height and base.
private Area1(int height, int base) {
this.height = height;
this.base = base;
}
When you call this,
Area1 square = new Area1(side1,side2);
square.getArea();
the getArea() method looks for the variables side1 and side2. But you have set the variables height and base only. So, the variables side1 and side2 have the value 0 by default.
Possible solution:
You should probably initialize the objects with the other constructor method that you already have, that uses all the variables, like
Area1 square = new Area1(side1,side2,0,0,0,0);
square.getArea();
Area1 triangle = new Area1(0, 0, height, base, 0, 0);
triangle.getArea1();
Area1 rectangle = new Area1(0,0,0,0,width,length);
rectangle.getArea2();
You made mistake in your parameterize constructor as you assign side1 and side2 value to this.height = height; this.base = base; and you trying to multiply side1 and side2 which has default value only.
private Area1(int side1, int side2) {
this.side1 = side1;
this.side2 = side2;
}
My teacher gave me this
In a n - sided regular polygon, all sides have the same length and all angles have the same degree. Design a class named RegularPolygon that contains:
A private int data field named n that defines the number of sides in the Polygon with default value 3.
A private double data field named side that stores the length of the side with default value 1.
A private double data field named X that defines the x - coordinate of the polygon’s center with default value 0.
A private double data field named Y that defines the y - coordinate of the polygon’s center with default value 0.
A constructor that creates a regular polygon with the specified number of sides, length of side, and x - and y- coordinates (values are passed from the parameters to the fields).
The accessor methods for all data fields.
The method getPerimeter() that returns the perimeter of the polygon.
The method getArea() that returns the area of the polygon. The formula is Area = n * s*s / (4 * tan(PI / n)).
2) Write a RegularPolygonTest class, allow the user to enter the data fields, and your program prints out the perimeter and the area of the regular polygon.
This is my code so far:
public class RegularPolygon{
private int n;
private double side, x, y;
public RegularPolygon(){
n = 3;
side = 1;
x = 0;
y = 0;
}
public RegularPolygon(int n, double side){
this.n = n;
this.side = side;
x = 0;
y = 0;
}
public RegularPolygon(int sn, double length, double x_coord, double y_coord){
n = sn;
side = length;
x = x_coord;
y = y_coord;
}
//set n to the user input
public void setN(int other){
n = other;
}
public int getN(){
return n;
}
//set side to userinput
public void setSide(double otherside){
side = otherside;
}
public double getSide(){
return side;
}
//set x to user input
public void setX(int x_co){
x = x_co;
}
public double getX(){
return x;
}
//set y to user input
public void setY(int they){
y = they;
}
public double getY(){
return y;
}
//find perimeter
public double getPerimeter(){
return n * side;
}
//find area
public double getArea(){
double s_squ = side * side;
double pin = Math.PI/n;
double tangent = Math.tan(pin);
return (n*s_squ)/(4*tangent);
}
}
import java.util.Scanner;
public class RegularPolygonTest{
public static void main(String[] args){
Scanner yer = new Scanner(System.in);
//number of sides
System.out.println("Enter number of sides: ");
int sn = yer.nextInt();
//length of sides
System.out.println("Enter length of sides: ");
double length = yer.nextDouble();
//x-coord
System.out.println("Enter the x-coordinate of the center: ");
double x_coord = yer.nextDouble();
//y-coord
System.out.println("Enter the y-coordinate of the center: ");
double y_coord = yer.nextDouble();
if (x_coord == 0 && y_coord == 0){
RegularPolygon rp = new RegularPolygon(sn, length);
}
else if (sn > 3 && length > 1){
RegularPolygon rp = new RegularPolygon(sn, length, x_coord, y_coord);
}
else{
RegularPolygon rp = new RegularPolygon();
}
System.out.println("The perimeter of the " + rp.getN() + "-sided polygon is : "+ rp.getPerimeter() +". And the are is : "+ rp.getArea());
}
}
The error I get is that the IDE can't find symbol and it points to all of the rp in the last line. How might I fix this error?
All the rp are inside blocks. You need to define a possibly uninitialized rp before the ifs and use this common rp within the ifs.
I am very new to java and I need a little help on a an assignment. The assignment was to get user input (radius, x-coordinate, & y-coordinate) to draw 3 different colored circles in a drawingPanel, I have that part down. The second part asks us for a static method that compares the radii of two circles and lets the user know if one is smaller, bigger, or the same size as the other. I am having trouble figuring out how to use the input for the radii in the method that compares the two.
Here is my code so far:
import java.awt.*;
import java.util.*;
public class Circles {
public static final Scanner CONSOLE = new Scanner(System.in);
public static void blueCircle(Graphics g) {
g.setColor(Color.BLUE);
int r = CONSOLE.nextInt();
int x = CONSOLE.nextInt();
int y = CONSOLE.nextInt();
g.fillOval(0 + x, 0 + y, r * 2, r * 2);
}
public static void greenCircle(Graphics g) {
g.setColor(Color.GREEN);
int r = CONSOLE.nextInt();
int x = CONSOLE.nextInt();
int y = CONSOLE.nextInt();
g.fillOval(0 + x, 0 + y, r * 2, r * 2);
}
public static void redCircle(Graphics g) {
g.setColor(Color.RED);
int r = CONSOLE.nextInt();
int x = CONSOLE.nextInt();
int y = CONSOLE.nextInt();
g.fillOval(0 + x, 0 + y, r * 2, r * 2);
}
public static void compareCircles(int r1, int r2) {
int x;
if (r1 < r2)
x = -1;
if (r1 == r2)
x = 0;
if (r1 > r2)
x = 1;
return;
}
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(400, 300);
Graphics g = panel.getGraphics();
System.out.println("Enter values for the radius, x , & y-coordinates of blue circle: ");
blueCircle(g);
System.out.println("Enter values for the radius, x , & y-coordinates of green circle: ");
greenCircle(g);
System.out.println("Enter values for the radius, x , & y-coordinates of red circle: ");
redCircle(g);
}
}
You can reduce the code reuse in your implementation. Make a generic method for create the circle for given input parameters.
public static void blueCircle(Graphics g, int r, int x, int y, Color c) {
g.setColor(c);
g.fillOval(0 + x, 0 + y, r * 2, r * 2);
}
Then one generic method for radii comparison.
public static String compareCircles(int r1, int r2) {
String output = "";
if (r1 < r2)
output = r1+" circle is smaller than "+r2;
if (r1 == r2)
output = "both circles are in same size.";
if (r1 > r2)
output = r1+" circle is larger than "+r2;
return output;
}
Now within the main mehod get the necessary inputs and pass them to these methods.
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(400, 300);
Graphics g = panel.getGraphics();
System.out.println("Enter values for the radius, x , & y-coordinates of blue circle: ");
int rBlue = CONSOLE.nextInt();
int xBlue = CONSOLE.nextInt();
int yBlue = CONSOLE.nextInt();
// Better to do the validation for the user inputs
blueCircle(g, rBlue, xBlue, yBlue, Color.BLUE);
// Do the same thing for the other two circles.
// Then call the comparison method with your r input values like below.
//String output = compareCircles(rBlue, rGreen);
}
Hope this is what you are looking for..
I'm trying to draw multiple objects in a canvas but keep getting a runtime error, below is my GraphicalObject Class:
/*import javafx.scene.canvas.Canvas;*/
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Scanner;
public class GraphicalObject { //class
private Scanner scan;
private int x;
private int y;
private int width;
private int height;
//private double xPoints;
//private double yPoints;
//private int numPoints;
/*
* constructor
*/
public GraphicalObject(){
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
//this.xPoints = 0;
//this.yPoints = 0;
//this.numPoints = 0;
}
public GraphicalObject(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
//think of get/set methods ie acessor/mutators
//get x
//get y
//get width
//get height
public void draw(GraphicsContext gc){
//int numPoints = 5;
//double[] xPoints = new double[numPoints];
//double[] yPoints = new double[numPoints];
//xPoints = [x + (width/2), x + width, x + .8333 width, x + .1667 width, x]
//yPoints = [y, y + (height/2), y + height, y + height, y + (height/2)]
` gc.setFill(Color.PINK);
gc.fillRect(this.x, this.y, this.width, this.height);
//gc.fillPolygon(xPoints, yPoints, numPoints);
}
}
I can't figure out where I'm going wrong, I think my for loops are okay... I'm able to draw one rectangle but if I say I want to draw two, it lets me input info for one object and then gives a runtime error. This is my canvas class:
/**
* A basic canvas object that can store and
* draw graphical object
*/
import javafx.scene.canvas.Canvas;
import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
import java.util.Scanner;
public class GraphicalObjectCanvas extends Canvas {
// data fields
// constants
public static final int C_WIDTH = 500;
public static final int C_HEIGHT = 500;
// instance variables
/**
* a scanner object to make the Canvas interactive
*/
private Scanner scan;
// TO DO: DECLARE YOUR ARRAY OF GRAPHICAL OBJECTS HERE
// (AND ANY OTHER INSTANCE VARIABLES YOU NEED TO MAINTAIN YOUR ARRAY)
GraphicalObject[] graphobs;
private int index;
private int numObjects;
/**
* Creates a canvas for drawing graphical objects
* with a size of C_WIDTHxC_HEIGHT pixels
*/
public GraphicalObjectCanvas() {
super(C_WIDTH, C_HEIGHT);
// initialize the scanner object
this.scan = new Scanner(System.in);
// find out how many objects the user wants to add
System.out.println("How many graphical objects do you want?");
numObjects = scan.nextInt();
// TO DO: DEFINE YOUR ARRAY OF GRAPHICAL OBJECTS HERE
graphobs = new GraphicalObject[numObjects];
// for each object they wanted to add...
for (int i = 0; i < numObjects; i++) {
this.add();
}
}
public void draw() {
// clear the picture
this.clear();
// get the graphics context from the canvas
GraphicsContext gc = this.getGraphicsContext2D();
// TO DO: LOOP THROUGH YOUR ARRAY OF GRAPHICAL OBJECTS
// AND TELL EACH ONE TO DRAW PASSING THE GRAPHICS CONTEXT (gc) AS INPUT
for(int i = 0; i < numObjects; i++){
graphobs[i].draw(gc);
System.out.println("Object drawn: " + i); //or 1??);
}
}
private void clear() {
GraphicsContext gc = this.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, C_WIDTH, C_HEIGHT);
}
private void add() {
System.out.println("What is the x location of the object?");
int x = scan.nextInt();
System.out.println("What is the y location of the object?");
int y = scan.nextInt();
System.out.println("What is the width of the object?");
int width = scan.nextInt();
System.out.println("What is the height of the object?");
int height = scan.nextInt();
// TO DO: USE THE INFORMATION ABOVE TO CREATE A NEW GRAPHICAL OBJECT
// AND ADD IT TO THE ARRAY OF OBJECTS.
GraphicalObject gob = new GraphicalObject( x, y, width, height);
graphobs[index] = gob;
index++;
// after adding new object, redraw the canvas
this.draw();
}
}
The problem is that you are invoking GraphicalObject.draw(GraphicsContext) on a null object.
This occurs because you call GraphicalObjectCanvas.draw() at the end of the GraphicalObjectCanvas.add() method and the for loop in GraphicalObjectCanvas.draw() uses numObjects to determine the range of indices. However, you have yet to create and allocate an object to all the indices of the array graphobs when this for loop executes.
To fix this, either do not invoke GraphicalObjectCanvas.draw() at the end of your GraphicalObjectCanvas.add() method, or change the for loop in GraphicalObjectCanvas.draw() to i < index rather than i < numObjects.
For example:
for(int i = 0; i < index; i++){
graphobs[i].draw(gc);
System.out.println("Object drawn: " + i); //or 1??);
}
The problem I'm having is that I don't know how to call the methods in main to make them work.
First I ask the user what shape does he wants to calculate it's area in the method "public static void userInput". The user has 3 options to select from.
Then there are the 3 methods, 1 for the circle, 1 for the triangle and 1 for the square.
public static void userInput(double radius, int base, int height, int side){
int object = Integer.parseInt(JOptionPane.showInputDialog(
"Enter 1 if you want to know the area of a circle."
\nEnter 2 if you want to know the area of a triangle.
\nEnter 3 if you want to know the area of a square"));
if (object == 1){
radius = Double.parseDouble(JOptionPane.showInputDialog("Enter the radius"));
}else if (object == 2){
base = Integer.parseInt(JOptionPane.showInputDialog("Enter the base"));
height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height"));
}else if (object == 3){
side = Integer.parseInt(JOptionPane.showInputDialog("Enter the side"));
}
}
public static double circle(){
double radio = 0;
double circle = (radio * radio) * 3.14159265358;
JOptionPane.showMessageDialog(null, "The circle area is "+circle);
return circle;
}
public static int triangle(){
int base= 0;
int height= 0;
int triangle = (base * height)/2;
JOptionPane.showMessageDialog(null, "The triangle area is "+triangle);
return triangle;
}
public static int square(){
int side = 0;
int square = side * side;
JOptionPane.showMessageDialog(null, "The square area is "+square);
return square;
}
public static void main(String[] args) {
circle(userInput(radius));
triangle(userInput(base, height));
square(userInput(side));
}
Can anyone recommend a good read about methods?
Hope this is what you looked for:
import javax.swing.JOptionPane;
public class NewClass1 {
public void userInput(int x){
if (x == 1){
double radius = Double.parseDouble(JOptionPane.showInputDialog("Enter the radius"));
System.out.println(circle(radius));
}else if (x == 2){
int base = Integer.parseInt(JOptionPane.showInputDialog("Enter the base"));
int height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height"));
System.out.println(triangle(base,height));
}else if (x == 3){
int side = Integer.parseInt(JOptionPane.showInputDialog("Enter the side"));
System.out.println(square(side));
}
}
public static double circle(double radio){
double circle = (radio * radio) * 3.14159265358;
JOptionPane.showMessageDialog(null, "The circle area is "+circle);
return circle;
}
public static int triangle(int base,int height){
int triangle = (base * height)/2;
JOptionPane.showMessageDialog(null, "The triangle area is "+triangle);
return triangle;
}
public static int square(int side){
int square = side * side;
JOptionPane.showMessageDialog(null, "The square area is "+square);
return square;
}
public static void main(String[] args) {
NewClass1 obj = new NewClass1();
int object = Integer.parseInt(JOptionPane.showInputDialog(
"Enter 1 if you want to know the area of a circle\nEnter 2 if you want to know the area of a triangle.\nEnter 3 if you want to know the area of a square"));
obj.userInput(object);
}
}
Try doing some practices on the Java Methods.
The code you have pasted in your question is incomplete but as per your question:
to Returning values from a method with multiple variables?
You can use object of a class to return multiple variables.