Constructor Overloading Error [duplicate] - java

This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
I cant figure out the errors! But while compiling it shows error. Please help me out.....
// This program is used to find the area of a circle and a rectangle
// through constructor overloading concept.
class area {
float radius;
int l , b;
public area(float r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class constadd {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}`

Use double instead of float.
import java.util.*;
import java.lang.*;
import java.io.*;
class area {
double radius;
int l , b;
public area(double r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class Ideone {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}

As Anik mentioned, either change the constructor to take double as a parameter instead of float or while calling this constructor use suffix 4.5 with 'f' to specify that you want to pass float i.e., new area(4.5f);

Related

Cannot find symbol [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I have not been able to get my methods working, as when I import classes from the same package it gives me an error. When I put them in the same package it gives me an error of "cannot find symbol", referring to the class/method I am trying to use in the second one. Like here for example, I can use variables from the other classes but it throws an error whenever I use a method. I have seen similar problems but none of them have helped me so far.
1st class:
package main;
/**
*
* #author Darias
*/
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
ball p;
p = new ball();
System.out.println("the ball weighs" +p.getlength);
}
}
2nd class:
package main;
public class ball {
float length;
float weight;
public ball()
{
length = 100;
weight = 250;
}
public ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getlength()
{
return length;
}
public float getweight()
{
return weight;
}
public void kickball()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
}
Note: it's properly indented in the program, here I was just having trouble passing it to text
The getlength is a function so use this way: "the ball weighs" +p.getlength()
and please use visibility syntax and be object oriented: private, protected etc.
public class Ball {
private float length;
private float weight;
public Ball()
{
this.length = 100;
this.weight = 250;
}
public Ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getLength()
{
return length;
}
public float getWeight()
{
return weight;
}
public void kickBall()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
You're missing the parentheses after p.getlength in Main.
System.out.println("the ball weighs" +p.getlength());

Java math error [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm working on a lab for programming principles II, and I have a class that makes a point, with methods for setting the point, and calculating the distance between other points. Calculating the distance worked fine when testing it with a runner for just the class, but when I make other classes that use it as an object, I'm getting an error with the distance formula.
import java.lang.Math;
public class MyPoint {
private double x;
private double y;
public MyPoint(double dubx, double duby)
{
x=dubx;
y=duby;
}
public void setX(double dub) {
x = dub;
}
public void setY(double dub) {
y = dub;
}
public double getX() {
return x;
}
public double getY()
{
return y;
}
public double distance (MyPoint otherPoint)
{
return Math.sqrt(Math.pow((otherPoint.getX()-getX()),2)+(Math.pow((otherPoint.getY()-getY()),2)));
}
public MyPoint midpoint(MyPoint otherPoint)
{
MyPoint point = new MyPoint((otherPoint.getX()+getX()/2),(otherPoint.getY()+getY())/2);
return point;
}
}
That's the class I'm getting the error on. The distance part is getting a null pointer exception.
Here's what I'm passing in:
import java.lang.Math;
public class MyTriangle
{
private MyPoint v1;
private MyPoint v2;
private MyPoint v3;
public MyPoint getPoint1()
{
return v1;
}
public MyPoint getPoint2()
{
return v2;
}
public MyPoint getPoint3()
{
return v3;
}
public void setPoint1(double x, double y)
{
v1= new MyPoint(x,y);
}
public void setPoint2(double x, double y)
{
v2 = new MyPoint(x,y);
}
public void setPoint3(double x, double y)
{
v2= new MyPoint(x,y);
}
public double getArea()
{
double a= v2.distance(v3);
double b= v1.distance(v3);
double c= v1.distance(v2);
double s= (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}
public class TestMyTriangle
{
public static void main(String [] args)
{
MyTriangle tr1 = new MyTriangle();
tr1.setPoint1(17,17);
tr1.setPoint2(5,30);
tr1.setPoint3(5,17);
System.out.println("Area:\t"+tr1.getArea());
}
}
And the error:
Exception in thread "main" java.lang.NullPointerException
at MyPoint.distance(MyPoint.java:34)
at MyTriangle.getArea(MyTriangle.java:37)
at TestMyTriangle.main(TestMyTriangle.java:9)
I can't seem to figure it out. Please help.
You get the Nullpointer because v3 is null:
fix with:
public void setPoint3(double x, double y)
{
v3= new MyPoint(x,y); // instead of v2
}
Another tipp: to calculate a square dont use Math.pow(x,2).
Altough it works.
The code is cleaner and faster if you use
x*x instead Math.pow(x,2);

aggregation in Java and methods in main method

I have such code which works well, but I wonder why method area is not possible to put in the main method
public class Circle {
Operation op;
double pi = 3.14;
double area(int radius) {
op = new Operation();
int rsquare = op.square(radius);
return rsquare * pi;
}
public static void main(String arg[]) {
Circle c = new Circle();
double s = c.area(5);
System.out.println(s);
}
class Operation {
int square(int n) {
return n * n;
}
}
}
Example which doesn't work:
public static void main(String arg[]) {
double area ( int radius){
op = new Operation();
int rsquare = op.square(radius);
return rsquare * pi;
}
The only way to nest method implementation code inside Java methods is with anonymous classes. In your case this would look like this (code has to be nested inside a class of sorts):
public static interface Circle {
double area(int radius);
}
public static interface Operation {
int square(int n);
}
public static void main(String arg[]) {
Circle c = new Circle() {
Operation op;
double pi = 3.14;
public double area(int radius) {
op = new Operation() {
public int square(int n) {
return n * n;
}
};
int rsquare = op.square(radius);
return rsquare * pi;
}
};
double s = c.area(5);
System.out.println(s);
}
Take
double area(){...}
Outside of the main method
Java doesn't support nested methods, even if it did, there would be no point to put a nonstatic method in a static method
The closest thing would be nested classes that contain methods, declared in the method

New to Java Programming, and trying to construct a Circle class with a tester main

So I'm in a beginning Java class and our second assignment is to create a circle class with a tester, and my first file, which is the public class Circle, looks like this
public class Circle
{
Circle ( ) { }
int r;
double area() {
return Math.PI*r*r;
}
double diameter( )
{
return 2 * r;
}
double circumference( ) {
return Math.PI * 2 * r;
}
int getR( ) {
return r;
}
}
This gave no compilation errors, so I thought I was hopefully on the right track. Then my Circle_test file gave some error I couldn't even spot.
Circle_test.java line 6: error: cannot find symbol
c.setR(1);
^
Circle_test.java line 11: error: cannot find symbol
c.setR(i)
^
class Main {
public static void main(String [ ] args) {
Circle c = new Circle( );
c.setR(1);
System.out.printf("a = %f\n", c.area( ));
double a;
for (int i = 0; i < 10; i++) {
c.setR(i);
a = c.area( );
}
}
}
Any help regarding as to why I'm getting these errors would be much appreciated, thank you.
You don't have a radius setter e.g.
public void setR(int r) {
this.r = r;
}
You may prefer to initialise r via the constructor e.g.
public Circle(int r) {
this.r = r;
}
such that you can't initialise a Circle without a radius. That a lot more reliable, and is a step towards making Circle an immutable class (meaning that it's unchangeable and thus more easily understandable)
You have to have a setter method for variable r in Circle class. Add the followinf method to Circle class
public void setR(int r) {
this.r = r;
}
Because you don't have setR method in your Circle class. Add this code in your Circle class
public void setR(int r) {
this.r = r;
}
OR
public Circle(int r) {
this.r = r;
}
Although you have getter methods for integer field r "getR()", you did not implement a setter method for r. "c.setR(1)" does not mean anything. You should implement a setR(int) method.
public void setR(int r){
this.r = r;
}
should be fine

Abstract Classes error

I have a problem with my program and I can't find the reason why thought. It points at the public static void main(String[] args) line nowhere else, can't figure it out :(
Tried checking for the braces, if by accident I missed one or two but still no, it is not an interface implementation so I don't have to set each method of the abstract class to public in the implementation...
abstract class Shape {
private String name;
Shape(String name0) {name = name0;}
abstract double area();
abstract double perim();
void put() {
System.out.println(name + " with area " + area()+ " and perimeter " + perim());
}
}
class Circle extends Shape{
private double r;
Circle(String name0, double inR) {
super(name0);
r = inR;
}
double area() {
return (Math.sqrt(r)*Math.PI);
}
double perim() {
return 2*(Math.PI * r);
}
}
class Rectangle extends Shape{
private double a,b;
Rectangle(String name0, double a0, double b0) {
super(name0);
a=a0; b=b0;
}
double area() {
return (a*b);
}
double perim() {
return 2*(a+b);
}
}
}
class TestClass {
public static void main(String args[]) {
Shape[] figures = {new Rectangle("Rectangle", 2.0, 3.0), new Rectangle("Square", 4.0, 4.0), new Circle("Circle", 2.0)};
for (Shape s: figures)
s.put();
}
}
You have an extra closing brace } before the main method. Just remove that.
Suggestion: It is really wise to use an IDE to do coding because you can get away from these compilation errors easily and quickly.

Categories

Resources