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;
}
Related
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.
This is my last homework for my java class and I have been trying to run this through a compiler but I don't understand whats wrong with the code.
I tried using void after reading about how that would fix the return type issue but that just made it worse, maybe I was putting void in the wrong place.
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Rectangle() {
}
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " +
rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " +
rectangle1.getPerimeter());
Rectangle rectangle2 = new Rectangle(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " +
rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " +
rectangle2.getPerimeter());
}
}
This is my last homework for this class I just want this over with any help is appreciated.
The constructor name should have the same name as the class name
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Exercise09_01() {
}
public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
}
public Rectangle() {
}
This is a method as far as Java is concerned. And all methods have to have a return type.
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
The same here.
You don't need the 1st one really unless you really need to be able to make one without those values set.
You can just rename them, but you probably just want to rename the class Rectangle
Whats wrong in your code is your class name and constructor names are different.
You have two options, one is to rename the constructor to Exercise01_01 or meke the return type of the Rectangle as void.
public class Exercise01_01 {
private double width = 1;
private double height = 1;
public Exercise01_01() {
}
public Exercise01_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Exercise01_01 rectangle1 = new Exercise01_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + rectangle1.getPerimeter());
Exercise01_01 rectangle2 = new Exercise01_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + rectangle2.getPerimeter());
}
}
public class Exercise09_01 {
private double width = 1;
private double height = 1;
public Exercise09_01() {
}
public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Exercise09_01 rectangle1 = new Exercise09_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " +
rectangle1.getPerimeter());
Exercise09_01 rectangle2 = new Exercise09_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " +
rectangle2.getPerimeter());
}
}
Thanks for all the help and this is the code that passes through the compiler. Just leaving it here for future visitors.
I'm trying to do my first methods.
I'm having trouble getting the perimeter to display the output as a String. I'm wondering why this is happening. I very well could have other problems inside of my code but the perimeter not outputting is what is holding me back right now.
Following is my code.
public class Polygon {
public Polygon() {
int numSides = 4;
double SideLength = 5.0;
double xCoord = 0.0;
double yCoord = 0.0;
double apothem = 5.0;
double perimeter = 20.0;
}
private int numSides = 2;
private double SideLength = 2;
private double xCoord;
private double yCoord;
private double apothem;
private double perimeter;
private double area;
public Polygon(int numsides, double sideLength, double xcoord, double ycoord, double Apothem, double Perimeter) {
SideLength = sideLength;
numSides = numsides;
xCoord = xcoord;
yCoord = ycoord;
apothem = Apothem;
perimeter = Perimeter;
}
public int getnumsides() {
return numSides;
}
public double getSideLength() {
return SideLength;
}
public double getxcoord() {
return xCoord;
}
public double getycoord() {
return yCoord;
}
public double getApothem() {
return apothem;
}
public double getPerimeter() {
return numSides * SideLength;
}
public void setsideLength(double ssideLength){
SideLength = ssideLength;
}
public void setnumsides(int snumsides){
numSides = snumsides;
}
public void setxcoord(double sxcoord){
xCoord = sxcoord;
}
public void setycoord(int sycoord){
yCoord = sycoord;
}
public void setApothem(int sApothem){
apothem = sApothem;
}
public void setPerimeter(int sPerimeter){
perimeter = sPerimeter;
}
public String toString() {
String str = numSides + " is the number of sides the polygon has and " + SideLength + " is how long the sides are. "+ xCoord + " is how long the x coordinate is and " + yCoord + " is how long the y coordinate is. " + apothem + " is the apothem of the polygon and " + perimeter + " is the perimeter of the polygon.";
return str;
}
public void getArea() {
area = .5 * apothem * perimeter;
}
}
You are again defining same field variables in Polygon() constructor which is not required because you have already defined them as private class members. This is the reason that some values are setting as default while printing toString() method.
Your Polygon() constructor should be look like this:
public Polygon() {
numSides = 4;
SideLength = 5.0;
xCoord = 0.0;
yCoord = 0.0;
apothem = 5.0;
perimeter = 20.0;
}
What do you mean by "perimeter not outputting"?
May be this is what you want to achieve?
public static void main(String[] args){
Polygon p = new Polygon();
double perimeter = p.getPerimeter();
System.out.println("Perimeter is " + perimeter);
}
Ok so for class I have been working on some oblong questions based off an oblong class. The question I am having an issue with is to create a method to increase the height and width of the oblong by a user defined amount.
This is my main:
import java.util.Scanner;
public class Oblong6
{
public static void main(String [] args)
{
Oblong ob1 = new Oblong();
Scanner keyboardIn = new Scanner(System.in);
Oblong ob = new Oblong();
double h, w, x;
System.out.print ("Enter the height of the oblong:");
h = keyboardIn.nextDouble();
System.out.print ("Enter the width of the oblong:");
w = keyboardIn.nextDouble();
System.out.print (" Enter the amount to increment by ");
x = keyboardIn.nextInt();
ob.setHeight(h);
ob.setWidth(w);
ob.setX(x);
System.out.println (ob.incHeight());
System.out.println (ob.incWidth());
System.out.println("Height " + h);
System.out.println("Width " + w);
}
}
And this is the method I have created in the oblong class to increase them:
public class Oblong
{
// instance variables
private double height;
private double width;
private double x;
// constructor
public Oblong()
{
height = 0.0;
width = 0.0;
x = 0.0;
}
// methods
public double getHeight()
{
return height;
}
public double getWidth()
{
return width;
}
public double setX(double x)
{
return x;
}
public void setWidth(double w)
{
width = w;
}
public void setHeight(double h)
{
height = h;
}
public double calculateArea()
{
return width * height;
}
public double calculatePerimeter()
{
return width + height * 2;
}
public boolean isSquare()
{
if(height == width)
{
return true;
}
else
{
return false;
}
}
public double incHeight()
{
{
return height + x ;
}
}
public double incWidth()
{
{
return width + x ;
}
}
}// end of class
but it only ever prints out the original height and width.
Your code:
public double incHeight()
{
{
return height + x ;
}
}
That just adds two numbers and returns the result.
It doesn't do anything else. The same is true for your other methods.
Whereas the purpose of the method seems to be to alter the state of the underlying object. But as said; the current implementation does not alter that state.
Hope that is good enough to help you to resolve your problem on your own.
Side note: read about Java syntax. Your extra pair of braces ... doesn't do anything either.
Whit this:
public double incWidth()
{
{
return width + x ;
}
}
You are returning width + 1 but you are not modifying the private attribute. Just do this:
public double incWidth(){
this.width = this.width + 1;
return this.width;
}
Also in setters you don't need to return anything. To change an attribute inside a class do something like:
private double value;
private void setValue( double value ) {
this.value = value;
}
With this.value you are refering to the private value inside the class. Without this you are refering to the parameter value of the method setValue.
Further reading:
How do getters and setters work?
Your instance variable x has not been set, hence the height + x will return height + 0.0.
change this:
public double setX(double x)
{
return x;
}
To this:
public double setX(double x)
{
this.x = x;
}
This will return value to be displayed, but it should be set for later use, so you need this :
public void incHeight()
{
setHeight(height + x) ;
}
and then :
System.out.println("Height " + height); // height not h
//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.