How to apply a value from one method to another? - java

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());
}

Related

How do I compare 2 objects with 1 parameter?

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()));
}

I think I have problem with static methods. How do I fix this error?

public class ConversionTester
{
public static void main(String[] args)
{
double g = gallonsToPints(4.0);
double f = feetToInches(2.0);
System.out.println();
System.out.println();
}
}
public class Conversion
{
public double gallon;
public double feet;
public static double gallonsToPints(double ga)
{
gallon = ga;
return gallon * 8;
}
public static double feetToInches(double fe)
{
feet = fe;
return feet * 12;
}
}
ConversionTester.java: Line 7: You may have forgotten to declare gallonsToPints(double) or it's out of scope.
ConversionTester.java: Line 8: You may have forgotten to declare feetToInches(double) or it's out of scope.
You have two options, either will work. The first is explicitly naming the class like
double g = Conversion.gallonsToPints(4.0);
double f = Conversion.feetToInches(2.0);
The second would be to add static import(s). Before public class ConversionTester like,
static import Conversion.gallonsToPints;
static import Conversion.feetToInches;

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

Finding Area Of Triangle In Java

public class RightTriangle
{
private double leg_1;
private double leg_2;
public RightTriangle ()
{
leg_1 = 1;
leg_2 = 1;
}
public RightTriangle (double s1, double s2)
{
leg_1= s1 ;
leg_2= s2 ;
}
public double findArea ()
{
double area= ((leg_1+leg_2)/2);
return area;
}
public double findPerimeter ()
{
double s3Squared= Math.pow(leg_1,2) + Math.pow( leg_2,2);
double s3= Math.sqrt(s3Squared);
double perimeter=(leg_1 + leg_2 + s3);
return perimeter;
}
public void dilate (double factor)
{
}
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
}
For my Java program, the constructor should construct a triangle by default with two legs with a length of one. The other constructor allows you to choose the length of the triangles two legs.
I'm trying to test my program's methods by running the "findArea" method with my t1 triangle object, however when I try to run the program I get a "identifier expeced after the token error with my my t1.findArea() code highlighted. Please help me fix this error.
You need to create a main class to get your program started. Also, this line:
t1.findArea();
needs to be placed inside a method. I propose you change the last two lines of code to this:
public static void main(String[] args) {
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
}
You have to add a main method to make your class runnable.
public static void main(String args[]) {
RightTriangle t1 = new RightTriangle(3, 4);
System.out.println(t1.findArea());
}
So the full code will be
public class RightTriangle {
private double leg_1;
private double leg_2;
public RightTriangle() {
leg_1 = 1;
leg_2 = 1;
}
public RightTriangle(double s1, double s2) {
leg_1 = s1;
leg_2 = s2;
}
public double findArea() {
double area = ((leg_1 + leg_2) / 2);
return area;
}
public double findPerimeter() {
double s3Squared = Math.pow(leg_1, 2) + Math.pow(leg_2, 2);
double s3 = Math.sqrt(s3Squared);
double perimeter = (leg_1 + leg_2 + s3);
return perimeter;
}
public void dilate(double factor) {
}
public static void main(String args[]) {
RightTriangle t1 = new RightTriangle(3, 4);
System.out.println(t1.findArea());
}
}
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
That needs to be in a method called through main
public static void main(String[] args)
{
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
}

Categories

Resources