Java error unable to identify mistake - java

This is Java code, I have created 4 classes 3 constructor and I am getting error of:
method area in class Rect cannot be applied to given types
There is a similar error for rest of 2 class as well. In this program basically I have created 4 classes, 1 for calculating area of rect, 1 for calculating area of Tri and 1 for calculating area of Square and last one is to access main function.
I have created 3 constructor for all the 3 classes rect tri and square and I am unable to spot the mistake in this program.
class Rect //1st class rect
{
double l, b; //variables
Rect(double l, double b) //constructor for rect
{
this.l = l;
this.b = b;
}
double area(double l, double b) //method to cal Rect area
{
return l * b;
}
}
class Square //square class
{
double s;
Square(Double s) //constructor for class
{
this.s = s;
}
double area(double s) //method to cal area for square
{
return s * s;
}
}
class Tri // class for triangle
{
double l, b, h; //variables
Tri(double l, double b, double h) // constructor for tri
{
this.l = l;
this.h = h;
this.b = b;
}
double area(double l, double b, double h) //method to cal area for tri
{
return 0.5 * l * b * h;
}
}
class Area3 {
public static void main(String args[]) {
Rect r = new Rect(10, 10); //constructor initialization for Rect
Square s = new Square(15.0);//constructor initialization for Square
Tri t = new Tri(10.0, 20.0, 30.0);//constructor initialization for Tri
System.out.print(" " + r.area() + "" + s.area() + "" + t.area()); //print areas
}
}

Your area method declarations state that the area methods take in arguments. With those declarations you can't say
Rect r = new Rect(1,4);
r.area();
Simply remove the double argument values from the area methods

You have to create area method without parameters, here the solution,
class Rect // 1st class rect
{
double l, b; // variables
Rect(double l, double b) // constructor for rect
{
this.l = l;
this.b = b;
}
double area(){
return this.l * this.b;
}
double area(double l, double b) // method to cal Rect area
{
return l * b;
}
}
class Square // square class
{
double s;
Square(Double s) // constructor for class
{
this.s = s;
}
double area(){
return this.s * this.s;
}
double area(double s) // method to cal area for square
{
return s * s;
}
}
class Tri // class for triangle
{
double l, b, h; // variables
Tri(double l, double b, double h) // constructor for tri
{
this.l = l;
this.h = h;
this.b = b;
}
double area(){
return 0.5 * this.l * this.b * this.h;
}
double area(double l, double b, double h) // method to cal area for tri
{
return 0.5 * l * b * h;
}
}
class Area3 {
public static void main(String args[]) {
Rect r = new Rect(10, 10); // constructor initialization for Rect
Square s = new Square(15.0);// constructor initialization for Square
Tri t = new Tri(10.0, 20.0, 30.0);// constructor initialization for Tri
System.out.print(" " + r.area() + " and " + s.area() + " and " + t.area()); // print
// areas
}
}
Hope this help, BTW it's work in my PC.

look at your contractors, they all receive an arguments.
and all your area()'s are getting also a arguments.
but!! in your main, you are calling the area() and do not give any values.
just delete from area()'s functions the receiving arguments.

Related

Size comparisons (circumference/square perimeter) etc

3 diffrent classes 1 for handling Circle isntances ,1 for Square instances and the 3rd for comparrisons between them(main) . In the main function i find the circle (between c1..c4) and square (between s1...s5) and print the biggest circumference and area of them respectively.[so circle-circle and square-square comparison]
!!!! NOTE : Only the ones with the biggers radius or sides have the biggest circumference or area , so i only use r and a for comparisons.i dont know if its possible to return this if i use the area/circumference method(no , cause then i will only handle numbers ?).Correct me please.
Now i want to print the characteristics(x,y,r/a) of the geometric shape (circle/square) with the biggest perimeter. How can i do this ? Where do i compare?New class?[square-circle comparison]
public class Circle {
public double x,y,r;
public double circumference() {
return 2*(3.14)*r;
}
public double area() {
return 3.14*r*r;
}
public Circle bigger(Circle c){
if(c.r>r) return c; else return this;
}
public Circle(double x, double y, double r) {
this.x=x;
this.y=y;
this.r=r;
}
}
public class Square {
public double x,y,a;
public double perimeter() {
return 4*a;
}
public double area() {
return a*a;
}
public Square bigger(Square s){
if(s.a>a) return s; else return this;
}
public Square(double x, double y, double a) {
this.x=x;
this.y=y;
this.a=a;
}
}
public class CircleAndSquareTest {
public static void main(String[] args) {
Circle c1 = new Circle(0.0,0.0,1.0);
Circle c2 = new Circle(1.0,0.0,2.0);
Circle c3 = new Circle(0.0,2.0,4.0);
Circle c4 = new Circle(1.0,3.0,1.0);
Circle cb = c1.bigger(c2).bigger(c3).bigger(c4);
System.out.println("The circle with the biggest circumference has:\n");
System.out.println("x-axis value: " + cb.x + " y-axis value: " + cb.y + " radius: " + cb.r+"\n");
Square s1 = new Square(0.0,0.0,1.0);
Square s2 = new Square(0.0,0.0,1.0);
Square s3 = new Square(0.0,0.0,5.0);
Square s4 = new Square(4.0,2.0,2.0);
Square s5 = new Square(0.0,0.0,1.0);
Square sb = s1.bigger(s2).bigger(s3).bigger(s4).bigger(s5);
System.out.println("The square with the biggest area has:\n");
System.out.println("x-axis value: " + sb.x + " y-axis value: " +
sb.y + " side: " + sb.a);
}
}
Here's how to do it using Comparators and the Collections class to find the max value. This is untested but it should do what you want. Note I'm using static inner classes here but they can be standard classes defined in their own file if needs be - this is just for the purpose of creating a quick answer.
public interface Shape {
double getPerimeter();
double getArea();
}
public static class PerimeterComparator implements Comparator<Shape> {
#Override
public int compare(Shape a, Shape b) {
return Double.compare(a.getPerimeter(), b.getPerimeter());
}
}
public static class AreaComparator implements Comparator<Shape> {
#Override
public int compare(Shape a, Shape b) {
return Double.compare(a.getArea(), b.getArea());
}
}
public static class Circle implements Shape {
private final double x, y, r;
#Override
public double getPerimeter() {
return 2 * (3.14) * r;
}
#Override
public double getArea() {
return 3.14 * r * r;
}
public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getR() {
return r;
}
}
public static class Square implements Shape{
private final double x, y, a;
#Override
public double getPerimeter() {
return 4 * a;
}
#Override
public double getArea() {
return a * a;
}
public Square(double x, double y, double a) {
this.x = x;
this.y = y;
this.a = a;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getA() {
return a;
}
}
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
List<Circle> circles = new ArrayList<>();
circles.add(new Circle(0.0,0.0,1.0));
circles.add(new Circle(1.0,0.0,2.0));
circles.add(new Circle(0.0,2.0,4.0));
circles.add(new Circle(1.0,3.0,1.0));
Circle largestCircle = Collections.max(circles, new PerimeterComparator());
System.out.println("The circle with the biggest circumference has:\n");
System.out.println("x-axis value: " + largestCircle.getX() + " y-axis value: " + largestCircle.getY() + " radius: " + largestCircle.getPerimeter() +"\n");
List<Square> squares = new ArrayList<>();
squares.add(new Square(0.0,0.0,1.0));
squares.add(new Square(0.0,0.0,1.0));
squares.add(new Square(0.0,0.0,5.0));
squares.add(new Square(4.0,2.0,2.0));
squares.add(new Square(0.0,0.0,1.0));
Square largestSquare = Collections.max(squares, new PerimeterComparator());
System.out.println("The square with the biggest area has:\n");
System.out.println("x-axis value: " + largestSquare.getX() + " y-axis value: " + largestSquare.getY() + " side: " + largestSquare.getA());
shapes.addAll(circles);
shapes.addAll(squares);
Shape largestPerimeter = Collections.max(shapes, new PerimeterComparator());
Shape largestArea = Collections.max(shapes, new AreaComparator());
System.out.printf("\nThe shape with the biggest perimeter is a %s and has has: a perimeter of: %f\n", largestPerimeter.getClass().getSimpleName(), largestPerimeter.getPerimeter());
System.out.printf("The shape with the biggest area is a %s and has has: an area of: %f\n", largestArea.getClass().getSimpleName(), largestArea.getArea());
}
Start by declaring a base interface, maybe called Shape that defines a method getPerimeterLength() for example.
Have all your shape classes implement that interface, and the corresponding method(s).
Now, a Square is also a Shape, and so is a Circle. Then you could put all these objects into an array of Shape. You iterate that array, and identify that entry with the maximum perimeter length. Then you simply call toString() on that object. Because you also overwrite the toString() method in all your classes to print the (different!) details each class has internally.

How do I access a Class in Java in my main class

How do I access my "Cone" class from my "Main" class. The error I am getting is the dot operator in my Main class. I'm really new to java so Im really confused as to how you access what I have created in the "Cone" class any details would be greatly appreciated. Thank you
class Cone {
public double r;
public double h;
public void setRadius() {
r = r;
}
public void setHeight() {
h = h;
}
public double volume(double r, double h) {
double v;
v = Math.PI * Math.pow(r, 2) * (h / 3);
return v;
}
public double surfaceArea(double r, double h) {
double sa;
sa = Math.PI * r * (r + Math.sqrt(Math.pow(h, 2) + Math.pow(r, 2)));
return sa;
}
}
class Main {
public static void main(String args[]) {
double r;
double h;
Cone cone = new Cone();
for (double i = 0; i < 10; i++) {
cone.volume(r);
cone.volume(h);
System.out.printf("Volume = %d\n", cone.Volume());
}
}
}
A quick fix for you
class Cone
{
public double r;
public double h;
public void setRadius(double r)
{
this.r = r;
}
public void setHeight(double h)
{
this.h = h;
}
public double volume()
{
double v;
v = Math.PI * Math.pow(r,2) * (h/3);
return v;
}
public double surfaceArea()
{
double sa;
sa = Math.PI* r * (r + Math.sqrt(Math.pow(h,2) + Math.pow(r,2)));
return sa;
}
}
class Main
{
public static void main(String args[ ])
{
double r;
double h;
Cone cone = new Cone();
for (double i = 0; i < 10; i++)
{
// r and h are not set yet
r = h = i; // maybe?
cone.setRadius(r);
cone.setHeight(h);
System.out.printf("Volume = %d\n", cone.volume( ));
}
}
}
I think your mistake is that cone.Volume() should be cone.volume(). And also, you need to provide parameters. I would modify the Cone class by getting rid of those set methods and adding a parameterized constructor like so:
public Cone(double r, double h)
{
//initialize fields
this.r = r;
this.h = h
}
first error update you code
public void setRadius(double r)
{
this.r = r;
}
public void setHeight(double h)
{
this.h = h;
}
You can set the values by setter, but in your code the setter for the variables don't have any parameter to accept:
public void setRadius(double r) {
this.r = r;
}
First off, you would probably want a constructor that can initialize the dimensions
public Cone(double r, double h){
this.r = r;
this.h = h;
}
Next, your volume method expects two double arguments
//use this
cone.volume(r,h);
//not this
cone.volume(r);
cone.volume(h);
Finally, since this method returns a double with the volume, you will want to have another double variable to catch it for later use
double v = cone.volume(r,h);
Declare your Main class as public. (Suggestion)
cone.volume() requires 2 parameters. You can not append 2 parameters one after another. Instead of cone.volume(r); cone.volume(h); You need to pass r and h in single statement like : cone.volume(r,h); Because you have mentioned original volume(par1, par2). Assign some value to r and h either by setter or by using constructor.
For a basic start hardcode some values to r and h in Main class only.

Encapsulation with Java

I'm trying to implement encapsulation in a program as part of some coursework however I've run into an error which I just can't seem to be able to fix with my limited knowledge which isn't helped by my Teacher/Lecturer who is very good at what he does however doesn't do very well when it actually comes to communicating the information, because of this could someone help me fix the error which is presented from the following program and explain to me why it's not working as intended.
class TwoDShapeEncap{
double width, height;
//Width
void setWidth(double w){
width = w;
}
double getWidth(){
return width;
}
//Height
void setHeight(double h){
height = h;
}
double getHeight(){
return height;
}
}
class Triangle extends TwoDShapeEncap{
String type;
private double sideA, sideB, sideC, adjacent, opposite;
//Side A
void setsideA(double a){
sideA = a;
}
double getsideA(){
return sideA;
}
//Side B
void setsideB(double b){
sideB = b;
}
double getsideB(){
return sideB;
}
//Side C
void setsideC(double c){
sideC = c;
}
double getsideC(){
return sideC;
}
//Adjacent
void setadjacent(double a){
adjacent = a;
}
double getadjacent(){
return adjacent;
}
//Opposite
void setopposite(double o){
width = o;
}
double getopposite(){
return opposite;
}
double getPerimeter(){
if(getsideB() == 0.0 && getsideC() == 0.0){
type = "equilateral";
return getsideA() * 3;
}
else if (getsideC() == 0.0){
type = "isosceles";
return getsideA() + getsideB() * 2;
}
else{
type = "scalene";
return getsideA() + getsideB() + getsideC();
}
}
//*******************************************************************************************
//* Paste the perimeter() and hypotenuse() methods from your previous class into this class *
//*******************************************************************************************
//***************************************
//* add an area method()into this class *
//***************************************
double area(double a, double b){
getWidth();
getHeight();
return (getWidth() * getHeight()/2);
}
}
class Rectangle extends TwoDShapeEncap{
boolean issquare;
private double height, width;
//Height
void setHeight(double h){
height = h;
}
double getHeight(){
return height;
}
//Width
void setWidth(double w){
width = w;
}
double getWidth(){
return width;
}
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
double area(double a, double b){
//getWidth();
//getHeight();
return (getWidth() * getHeight()/2);
}
boolean testSquare(double h, double w){
//getHeight();
//getWidth();
if (getHeight() == getWidth())
issquare = true;
else issquare = false;
return issquare;
}
//*********************************************
//* add area and perimeter methods this class *
//*********************************************
//*************************************************************************
//* add a testSquare method to test if a particular rectangle is a square *
//*************************************************************************
}
//Add a circle class which includes area and circumference methods
class Circle extends TwoDShapeEncap{
double radius, diameter;
double area (double r){
radius = r;
return Math.PI * (radius * radius);
}
double perimeter (double r){
radius = r;
return 2 * (Math.PI * radius);
}
}
class TwoDShapeEncapDemoNew {
public static void main(String args[]) {
//Triangle
Triangle t = new Triangle();
t.setsideA(5.7);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA() );
System.out.println("The type is " + t.type);
System.out.println();
t.setsideB(7.3);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA() );
System.out.println("If sideB is " + t.getsideB() );
System.out.println("The type is " + t.type);
System.out.println();
t.setsideC(2.7);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA());
System.out.println("If sideB is " + t.getsideB());
System.out.println("If sideC is " + t.getsideC());
System.out.println("The type is " + t.type);
System.out.println();
//Rectangle
Rectangle r = new Rectangle();
r.setHeight(7.8);
r.setWidth(4.2);
System.out.println("The perimeter is " + r.perimeter());
System.out.println("The");
}
}
Error message:
Main.java:186: error: method perimeter in class Rectangle cannot be applied to given types; System.out.println("The perimeter is " + r.perimeter()); ^ required: double,double found: no arguments reason: actual and formal argument lists differ in length 1 error –
When you call:
System.out.println("The perimeter is " + r.perimeter());
in r.perimeter you must pass two parameters (as your signature wants)
Your method in Rectangle class:
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
So fix:
System.out.println("The perimeter is " + r.perimeter(r.getHeight(), r.getWidth()));
You also can fix your method perimeter without parameters because in the body you use getHeigth() and getWidth() properties
So you can write:
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
The method perimeter in the class Rectangle expects two parameters, and you're not passing any. You could either call it like this:
r.perimeter(7.8,4.2);
Or redefine the method so it looks like this:
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
Thats because you are defining the perimeter function like this:
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
and calling System.out.println("The perimeter is " + r.perimeter()); with no parameters.
Since you are not really using double h and double w for nothing, you just have to remove them from the method definition
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
Since everybody is just facing the problem with the parameters I will face this problem: Getters are used to get the values of private fields if you're "outside" your class! If you're in a method in your class you don't have to use the getters, you can just use the variables themselfs:
Example:
public class SomeClass {
private int a;
public void setA(int anotherA) {
a = anotherA;
}
public int getA() {
return a;
}
public int getSquareOfA() {
// You don't use getA() to get the value now
// but you use a itself!
return a*a; // instead of 'return getA() * getA();'
}
}
You do have that problem at several points in your code!
According to your problem:
Your problem was that you're calling a method which has 2 parameters without any input parameters!
You can either remove the parameters of the method (which will be the logically right thing to do in your case), OR you pass some parameters.
In your specific case that means, change your perimiter() method as follows:
double perimiter() {
return (height + width) * 2;
// or if you want to impress your teacher ;) :
// return (height + width) << 1
}
Also you should change that methodname to getPerimiter() to keep up with your own naming conventions!
Modify your signature to remove the arguments.
class Rectangle extends TwoDShapeEncap{
///...
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
should be
class Rectangle extends TwoDShapeEncap{
///...
double perimeter(){
//Notice that you don't need to pass in these arguments
//as this function gets these arguments by itself.
return getHeight() * 2 + getWidth() * 2;
}

Simple Cartesian coordinate calculator

I'm trying to subtract the coordinates of 2 vectors but I'm a beginner who can't figure out the OOP code I need. This is what I have so far.
public class practice {
public static class vector{
int a;
int b;
public vector(int a, int b){
this.a = a;
this.b = b;
}
public String coordinate(int x, int y){
x = this.a - a;
y = this.b - b;
return x + " " + y;
}
}
public static void main(String[] args) {
vector vec1 = new vector(2,3);
vector vec2 = new vector(3,4);
vector.coordinate?
}
}
How can I subtract the ints from the 2 vector objects?
I have done here some basic example which will allow you to subtract one vector from another one.
package com.test;
public class Vector {
private int x;
private int y;
public Vector(int x, int y) {
this.x = x;
this.y = y;
}
/**
* #return the x
*/
public int getX() {
return x;
}
/**
* #return the y
*/
public int getY() {
return y;
}
// if you don't want to create new vector and subtract from it self, then return type of this method would be void only.
public Vector subtract(Vector other) {
return new Vector(this.x - other.x, this.y - other.y);
}
#Override
public String toString() {
return this.x + " : "+ this.y;
}
public static void main(String[] args) {
Vector vector1 = new Vector(10, 10);
Vector vector2 = new Vector(5, 5);
Vector vector3 = vector1.subtract(vector2);
System.out.println(vector3);
}
}
I think you just want to call the coordinate method from vec1 using data from vec2.
if so this is one way of doing it:
System.out.println(vec1.coordinate(vec2.a,vec2.b));
in this case you also need to change your coordinate method to:
public String coordinate(int x, int y){
x = this.a - x;
y = this.b - y;
return x + " " + y;
}
You can create a method XsubtractY for which you input two vectors, then inside it gets each vectors a and b and subtracts them from each other to return your new vector. As follows:
public class practice {
public static class vector{
int a;
int b;
public vector(int a, int b){
this.a = a;
this.b = b;
}
public static vector XsubtractY(vector x, vector y){
vector newVector = new vector(x.a-y.a, x.b - y.b);
return newVector;
}
}
public static void main(String[] args) {
vector vec1 = new vector(2,3);
vector vec2 = new vector(3,4);
vector newVector = vector.XsubtractY(vec1, vec2);
System.out.println("New vector a: " + String.valueOf(newVector.a));
/* New vector a: -1 */
System.out.println("New vector b: " + String.valueOf(newVector.b));
/* New vector b: -1 */
}
}
The new vector becomes (-1,-1)

Static method to turn shapes red

so i made this program and i am trying to add in my testing file a static method that turns a random array of shapes all "red".
public abstract class Shape
shape class
public abstract class Shape
{
private String color;
public Shape() { color = "white";}
public String getColor() { return color;}
public void setColor(String c) { color = c; }
public abstract double area();
public abstract double perimeter();
public abstract void display();
}
circle class
public class Circle extends Shape {
private double radius;
public Circle( double r)
{
super();
radius = r;
}
public double getRadius()
{ return radius; }
//Implement area, perimeter and display
public double area()
{
return Math.PI * radius* radius;
}
public double perimeter()
{
return 2* Math.PI *radius;
}
//Circle class - continued
public void display()
{
System.out.println( this);
}
public String toString()
{
return "Circle: radius:" + radius
+ "\tColor: " + getColor();
}
}
my main class for testing
public class TestingShapes {
public static double sumArea( Shape[] b)
{
double sum = 0.0;
for( int k = 0; k < b.length; k++)
{
sum = sum + b[k].area();
}
return sum;
}
public static void printArray( Shape[] b)
{
for (Shape u: b)
System.out.println(u + "\tArearea " + u.area());
System.out.println();
}
public static void main( String[] args)
{
Shape[] list = new Shape[20]; //Not creating Shapes
for ( int k = 0 ; k < list.length; k++)
{
double z = Math.random();
if( z < 0.33 )
list[k] = new Circle(1 + Math.random() * 10);
else if(z<0.66)
list[k] = new Rectangle ( 3*(k+1), 4*(k+1), 5*(k+1),6*(k+1));
else
list[k] = new Triangle ( 3*(k+1), 4*(k+1), 5*(k+1));
}
printArray(list);
System.out.println();
double sum = sumArea(list);
System.out.println("Sum of List Area: " + sum);
}
To turn some shapes randomly red, you would create a method accepting the array of shapes, and loop over it. You can use Math.random() to get a random floating point number between 0 and 1. To turn, say, 20% of shapes red, you would just compare Math.random() to 20%: if (Math.random() < 0.2) { call the shape's setColor method with "red" }. Since arrays/collections in Java are passed by reference, you don't need to return anything from the method, it will modify the copy that the caller owns.

Categories

Resources