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
Related
I made a java program that compares the distance between 2 objects
I managed to figure out how to solve it when I make a class with 2 parameters and compare these with the formula seen below;
public class RasterServices {
public static double distance (SimpleRasterElement a, SimpleRasterElement b) {
double d;
d = Math.sqrt(((b.x-a.x)*(b.x-a.x)) + ((b.y-a.y)*(b.y-a.y)));
return d;
}
public class SimpleRasterElement {
public int id;
public double x;
public double y;
public double height;
}
public class SimpleRasterElementTest {
public static void main (String[] args)
{
SimpleRasterElement a, b ; // Deklarera variabeln
a = new SimpleRasterElement (); // Skapa en instans (med ’new’),
b = new SimpleRasterElement ();
// Tilldela variablerna i ’a’ värden:
a.id = 1;
a.x = 6.0;
a.y = 8.0;
a.height = 10.5;
// Tilldela variablerna i ’b’ värden:
b.id = 1;
b.x = 9.0;
b.y = 12.0;
b.height = 15.5;
System.out.println (RasterServices.distance(a,b));
}
}
I can then test this via using my test program RasterServices.distance(a,b)
But now I want to make my variables private, use getters and create the distance() method within the RasterElement-class, and now I'm hardstuck.
public class RasterElementTest {
public static void main(String[] args) {
RasterElement re_a = new RasterElement(1, 6.0, 8.0, 10.5);
RasterElement re_b = new RasterElement(1, 9.0, 12.0, 15.5);
double d = re_a.distance(re_b);
System.out.println(d);
}
}
asd
public class RasterElement {
private final int id;
private final double x;
private final double y;
private final double height;
public RasterElement (int id_nr, double x_val, double y_val, double height_val) {
id = id_nr;
x = x_val;
y = y_val;
height = height_val;
}
public int getId () {
return id;
}
public double getX () {
return x;
}
public double getY () {
return y;
}
public double getHeight () {
return height;
}
public double distance (RasterElement a) {
double d;
d = Math.sqrt(((b.getX()-a.getX())*(b.getX()-a.getX())) + ((b.getY()-a.getY())*b.getY()-a.getY()));
return d;
}
}
But here in distance() i'm only allowed ONE parameter, can someone please explain to me how I can compare two elements/objects when I'm only allowed one parameter in the function?
(By using re_a.distance(re_b); in my test-code)
Thanks, sorry for the long post B-)
(how do I get the b-value into the equation in the method distance in the class RasterElement..?)
Change b to this. Also, you can eliminate d and return directly. Like,
public double distance (RasterElement a) {
return Math.sqrt(((this.getX()-a.getX())*(this.getX()-a.getX()))
+ ((this.getY()-a.getY())*this.getY()-a.getY()));
}
public class Store {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one*two*three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne,sideTwo,sideThree);
System.out.println(mult.area);
}
}
Can anyone help a beginner understand why, when passing parameters, this is an invalid method declaration?
You define/call a Calc constructor, but there is no Calc class.
Rename your class to Calc ant your code will compile and execute correctly:
public class Calc {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one * two * three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne, sideTwo, sideThree);
System.out.println(mult.area);
}
}
Cant set radius to count square: there're two methods, but how to force Java to count my square through radius that is random from randomNumber() ?
public class Figure {
public double randomNumber() {
return Math.random() * 11;
}
public double circleArea () {
return Math.PI*Math.pow(r,2); //how to assign a variable r in order to connect to randomNumber()??
}
public static void main(String[] args) {
Figure figure = new Figure();
System.out.println("r="+figure.randomNumber()+", s="+figure.circleArea());
}
}
Few different ways. You need to choose what works best for your scenario:
You can call the method from where required.
public double circleArea () {
return Math.PI*Math.pow(randomNumber(),2);
}
You can change the method to include a variable.
public double circleArea (double r) {
return Math.PI*Math.pow(r,2);
}
public static void main(String[] args) {
Figure figure = new Figure();
double r = figure.randomNumber();
System.out.println("r=" + r + ", s=" + figure.circleArea(r));
}
All you have to do is defining a private variable that holds the r value
public class Figure {
private double r;
public double randomNumber() {
r = Math.random() * 11;
return r;
}
public double circleArea () {
return Math.PI*Math.pow(r,2); //how to assign a variable r in order to connect to randomNumber()??
}
public static void main(String[] args) {
Figure figure = new Figure();
System.out.println("r="+figure.randomNumber()+", s="+figure.circleArea());
}
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);
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