I have an assignment for school where I have to create a program that will calculate the area of a circle given four points, but when I invoke the method areaCircle in main, nothing happens. It doesn't calculate the area.
public static void main(String[] args) {
Scanner reader;
reader = new Scanner(System.in);
System.out.println("Enter the coordinates of a point on the outside of a circle.");
System.out.println("x-coordinate: ");
int x1 = reader.nextInt();
System.out.println("y-coordinate: ");
int y1 = reader.nextInt();
System.out.println("Enter the center point of the circle.");
System.out.println("x-coordinate: ");
int x2 = reader.nextInt();
System.out.println("y-coordinate:");
int y2 = reader.nextInt();
areaCircle(x1, y1, x2, y2);
}
public static double distance(int x1, int y1, int x2, int y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx * dx + dy * dy;
double result = Math.sqrt(dsquared);
return result;
}
public static double areaCircle(int radius, double area) {
area = Math.PI * (radius * radius);
return area;
}
public static double areaCircle(int x1, int x2, int y1, int y2) {
double radius = distance(x1, y1, x2, y2);
double area = Math.PI * (radius * radius);
return area;
}
You need only to print the result, at the moment you are only calculating it.
Instead of
areaCircle (x1, y1, x2, y2);
write
System.out.println("The area is: " + areaCircle (x1, y1, x2, y2));
Related
I am writing a recursion code for my triangle drawing. For some reason, it wont do the recursion, but it will do the base drawing. Would like some help in figuring out why my code isn't repeating. Also, if someone could help me figure out how to fill the triangles with a color, that would be much appreciated.
public class Triangle {
public static void draw(int n, double size, double x, double y) {
if (n == 0) return;
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenColor(StdDraw.BLACK);
//StdDraw.line(xy, size/2)
double x1 = x + size/2;
double y1 = y + size/2;
double x2 = x1+ size/2;
// bottom triangle
StdDraw.line(x,y, x1, y1);
StdDraw.line(x1, y1, x2, y );
StdDraw.line(x2, y, x1, y);
//right triangle
StdDraw.line(y, y, x2, x2);
StdDraw.line(x1, x1, x1, y1);
// top triangle and left triangle
StdDraw.line(y1, x1, x, x2);
StdDraw.line(x2, x2, x, y);
draw(n-1, size -3, x, y);
}
public static void main(String[] args) {
double x = 0.0;
double y = 0.0;
double size = 1.0;
int n = 2;
draw(n, size, x, y);
}
}
My code works. It is giving me the correct result, but I feel I did a mistake because I declare x1, y1, x2, y2 too often (globally and locally). Am I? If I, however, delete one of the declarations, it does not work anymore. Error message:
error: cannot find symbol
Maybe someone can explain to me, how I should have solved the problem without declaring x1, y1, x2, y2 that often.
public class Distanz {
public static void main(String[] args) {
double d = 0;
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
berechneDistanzAlsProzedur(x1, x2, y1, y2);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(d));
}
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
x1 = 10;
y1 = 8;
x2 = 2;
y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
You can simply use the parameters x1, y1, x2, and y2 instead. Otherwise, your methods will always return the same value regardless of the arguments it was called with.
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
You could go simple and declare global variables like this:
public class Distanz {
public static void main(String[] args) {
double d = 0;
berechneDistanzAlsProzedur();
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: "+berechneDistanzAlsFunktion(d));
}
static double x1 = 10;
static double y1 = 8;
static double x2 = 2;
static double y2 = 12;
public static void berechneDistanzAlsProzedur() {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
Of course, I might not know what you want at all and my answer could be completely useless but I think I answered it pretty well. :D
Below solution can eliminate static declaration and code will look very neat.
public class Distanz {
public static void main(String[] args) {
Point point = new Point(10, 8, 2, 12);
berechneDistanzAlsProzedur(point);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(point));
}
public static void berechneDistanzAlsProzedur(Point point) {
double sqd_d = (point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2());
//This formula is same which you have used in berechneDistanzAlsFunktion method, you can eliminate berechneDistanzAlsProzedur function and directly call berechneDistanzAlsFunktion in main function
//You can pass different points to validate different result
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(Point point) {
return (Math.sqrt((point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2())));
}
}
Your Point class should be look like this:
public class Point {
private final double x1;
private final double y1;
private final double x2;
private final double y2;
public Point(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public double getX1() {
return x1;
}
public double getY1() {
return y1;
}
public double getX2() {
return x2;
}
public double getY2() {
return y2;
}
}
Here Point.java called as Wrapper class, which is holding Points information in this code.
Happy Coding.
I have been trying to write a simple but flexible class that holds some values of generic type T.
T extends Number, which means I just want this class to deal with everything from bytes to longs.
I am not all that familiar with how to use generics, so my main question to you guys is if there's a way to shorten the following set of functions into one function in order to reduce the unnecessary code duplication. The following is the given code:
public static byte distanceSq(byte x1, byte y1, byte x2, byte y2) {
x1 -= x2;
y1 -= y2;
return (byte) (x1 * x1 + y1 * y1);
}
public static short distanceSq(short x1, short y1, short x2, short y2) {
x1 -= x2;
y1 -= y2;
return (short) (x1 * x1 + y1 * y1);
}
public static int distanceSq(int x1, int y1, int x2, int y2) {
x1 -= x2;
y1 -= y2;
return (int) (x1 * x1 + y1 * y1);
}
public static float distanceSq(float x1, float y1, float x2, float y2) {
x1 -= x2;
y1 -= y2;
return (float) (x1 * x1 + y1 * y1);
}
public static double distanceSq(double x1, double y1, double x2, double y2) {
x1 -= x2;
y1 -= y2;
return (double) (x1 * x1 + y1 * y1);
}
public static long distanceSq(long x1, long y1, long x2, long y2) {
x1 -= x2;
y1 -= y2;
return (long) (x1 * x1 + y1 * y1);
}
I have tried to write something along the lines of:
public static <U extends Number> U distanceSq(U x1, U y1, U x2, U y2) {
x1 -= x2;
y1 -= y2;
return (x1 * x1 + y1 * y1);
}
However, since the variables are objects now, the operators cannot resolve them. I tried to convert them into their appropriate wrapper using an instanceof statement, but that got me nowhere either.
You can define your method like this
public static <T extends Number> Number distanceSq(T x1,T y1,T x2,T y2){
double x = x1.doubleValue() - x2.doubleValue();
double y = y1.doubleValue() - y2.doubleValue();
return (x * x + y * y);
}
And it can be called as
Integer r1 = distanceSq(a1, b1, a2, b2).intValue();
Byte r2 = distanceSq(x1, y1, x2, y2).byteValue();
I understand the concept of overloading and I'd like to think I have done it succesfully in this program; it runs fine but the output is not correct.
The program is supposed to calculate the area of a circle from two points, one being the radius and the other being a random point on the outside of the circle. The two points are given by the user, and each point consists of two numbers. So point one is x1, x2, while point two is y1, y2.
I did a test run by inputting the numbers 1, 2, 3, and 4, which should give me an answer of 3.1458....(pi). However, it gives me 25.132741228718352.
Any help on figuring out what is giving me this weird output would be much appreciated.
Here is the code
import java.util.Scanner;
public class AreaCircle {
static Scanner input = new Scanner(System.in);
public static double getDistance(double x1, double y1,
double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double distanceSquared = dx * dx + dy * dy;
double radius = Math.sqrt(distanceSquared);
return radius;
}
public static double areaCircle (double radius){
double area = (double)Math.PI * (radius * radius);
return area;
}
public static double areaCircle (double x1, double x2,
double y1, double y2) {
double radius = getDistance(x1, x2, y1, y2);
double area = areaCircle (radius);
return area;
}
public static void main(String[] args) {
System.out.print("Please input two points, with the first being \n"
+ "the middle of the circle and the other being \n"
+ "a point on the outside of the circle. These two points will \n"
+ "be used to find the area of your circle. \n\n"
+ "Input the first point here: ");
double x1 = input.nextDouble();
System.out.print("Input the second point here: ");
double x2 = input.nextDouble();
System.out.print("Input the third point here: ");
double y1 = input.nextDouble();
System.out.print("Input the fourth point here: ");
double y2 = input.nextDouble();
double result = areaCircle(x1, x2, y1, y2);
System.out.println("Your result is: " + result);
}
}
You're actually calculating the distance between (1, 2) and (3,4), since you've switched x2 with y1 in distance (compare it with the area function -- you'll see what I mean).
The distance between (1,2) and (3,4) is sqrt 8, when you substitute that into the formula, it gives an area of 8 * pi ~= 25.
I'm trying to draw Sierpinski's Triangle recursively in Java, but it doesn't work, though to me the logic seems fine. The base case is when the triangles are within 2 pixels of each other, hence the use of the Distance Formula.
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Canvas;
public class Triangle extends Canvas implements Runnable
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public Triangle()
{
setBackground(Color.WHITE);
}
public void paint( Graphics window )
{
window.setColor(Color.BLUE);
window.setFont(new Font("ARIAL",Font.BOLD,24));
window.drawString("Serpinski's Gasket", 25, 50);
triangle(window, (WIDTH-10)/2, 20, WIDTH-40, HEIGHT-20, 40, HEIGHT-20, 4);
}
public void triangle(Graphics window, int x1, int y1, int x2, int y2, int x3, int y3, int r)
{
//if statement base case
//midpoint = (x1 + x2 / 2), (y1 + y2/ 2)
if(Math.sqrt((double)(Math.pow(x2-x1, 2)) + (double)(Math.pow(y2-y1, 2))) > 2)
//if(r==0)
{
window.drawLine(x1, y1, x2, y2);
window.drawLine(x2, y2, x3, y3);
window.drawLine(x3, y3, x1, y1);
}
int xa, ya, xb, yb, xc, yc; // make 3 new triangles by connecting the midpoints of
xa = (x1 + x2) / 2; //. the previous triangle
ya = (y1 + y2) / 2;
xb = (x1 + x3) / 2;
yb = (y1 + y3) / 2;
xc = (x2 + x3) / 2;
yc = (y2 + y3) / 2;
triangle(window, x1, y1, xa, ya, xb, yb, r-1); // recursively call the function using the 3 triangles
triangle(window, xa, ya, x2, y2, xc, yc, r-1);
triangle(window, xb, yb, xc, yc, x3, y3, r-1);
}
public void run()
{
try{
Thread.currentThread().sleep(3);
}
catch(Exception e)
{
}
}
}
The Runner is
import javax.swing.JFrame;
public class FractalRunner extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public FractalRunner()
{
super("Fractal Runner");
setSize(WIDTH+40,HEIGHT+40);
getContentPane().add(new Triangle());
setVisible(true);
}
public static void main( String args[] )
{
FractalRunner run = new FractalRunner();
}
}
To me this should work but it causes a runtime/StackOverFlow error that I don't know how to correct. Any help?
You need to move the recursive calls to triangle, and the associated math, inside the conditional check on the separation. Right now, it will always call it and therefore you get the stack overflow.
Chances are your base case might not be working properly- what if the distance between two triangles is never two pixels? say we star with y1 and x1 being 0 and 200. their midpoint would be 100, then 50, 25, 12, 6, 3, 1<--- never hits the 2 pixel base case...
"StdDraw" was taken from here:
public class Sierpinski {
public static void sierpinski(int n) {
sierpinski(n, 0, 0, 1);
}
public static void sierpinski(int n, double x, double y, double size) {
if (n == 0) return;
//compute triangle points
double x0 = x;
double y0 = y;
double x1 = x0 + size;
double y1 = y0;
double x2 = x0 + size / 2;
double y2 = y0 + (Math.sqrt(3)) * size / 2;
// draw the triangle
StdDraw.line(x0, y0, x1, y1);
StdDraw.line(x0, y0, x2, y2);
StdDraw.line(x1, y1, x2, y2);
StdDraw.show(100);
//recursive calls
sierpinski(n-1, x0, y0, size / 2);
sierpinski(n-1, (x0 + x1) / 2, (y0 + y1) / 2, size / 2);
sierpinski(n-1, (x0 + x2) / 2, (y0 + y2) / 2, size / 2);
}
// read in a command-line argument n and plot an order Sierpinski Triangle
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
StdDraw.setPenRadius(0.005);
sierpinski(n);
}
}
Guy