Doing a what I thought would be a very very easy output lab for AP, I thought I was doing everything correctly, but the output keeps coming out as "The area is :: " + 0.0" when the "0.0" is supposed to be the calculated area of the circle. Here are the two classes:
Circle Class:
public class Circle
{
private double radius;
private double area;
public void setRadius(double rad)
{
rad = radius;
}
public void calculateArea( )
{
area = (3.14159*(radius*radius));
}
public void print( )
{
System.out.println("The area is :: " + area);
}
}
Circle Runner Class:
public class CircleRunner
{
public static void main( String[] args )
{
Circle test = new Circle ( );
test.setRadius(7.5);
test.calculateArea( );
test.print( );
test.setRadius(10);
test.calculateArea( );
test.print( );
test.setRadius(72.534);
test.calculateArea( );
test.print( );
test.setRadius(55);
test.calculateArea( );
test.print( );
}
}
Thanks!
Your setter is wrong, so your radius stays 0, it should be
public void setRadius(double rad) {
radius = rad;
}
You are assigning the variables in reverse order. It should be:
radius = rad;
And not:
rad = radius;
You need to use this to reference your radius in your setRadius Method. or swap the two values. You should assign " rad " to " radius" and not vis versa, since you will be recieving rad from the method.
public class Circle {
private double radius;
private double area;
public Circle() {
}
public void setRadius(double rad) {
this.radius = rad;
}
public void calculateArea() {
area = (3.14159*(radius*radius));
}
public void print() {
System.out.println("The area is :: " + area);
}
}
Related
My first question here so please point out my mistakes. And to be honest I couldn't find any similar questions.
So I am trying to write something very basic and I really can't figure why I can't call a method from my Util Class in my Main class.
Util class
private double accumulatedArea;
private double accumulatedCircumference;
public static double getAccumulatedArea(Shape[] shapes) {
double accumulatedArea = 0;
for (Shape s : shapes) {
accumulatedArea = accumulatedArea + s.area();
return accumulatedArea;
}
return accumulatedArea;
}
public static double getAccumulatedCircumference(Shape[] shapes) {
double accumulatedCircumference = 0;
for (Shape q : shapes) {
accumulatedCircumference = accumulatedCircumference + q.circumference();
return accumulatedCircumference;
}
return accumulatedCircumference;
}
public String toString() {
return "Number: ";
}
}
and Main class
public static void main(String[] args) {
Shape[] shapes = new Shape[4];
shapes[0] = new Circle(100);
shapes[1] = new Circle(23);
shapes[2] = new Rectangle(10, 20);
shapes[3] = new Rectangle(8, 12);
System.out.println("Area of the circle 1: " + shapes[0].area() + " and its circumference is: " + shapes[0].circumference());
System.out.println("Area of the circle 2: " + shapes[1].area() + " and its circumference is: " + shapes[1].circumference());
System.out.println("Area of the rectangle 1: " + shapes[2].area() + " and its circumference is: " + shapes[2].circumference());
System.out.println("Area of the rectangle 2: " + shapes[3].area() + " and its circumference is: " + shapes[3].circumference());
//Small test
System.out.println("Number of shapes are in the system: " + shapes.length);
//????? What to do here??
for (int p = 0; p < shapes.length; p++) {
Shape[p].getAccumulatedArea();
}
}
}
How can I call my getAccumulatedArea and getAccumulatedCircumference methods??
Been searching for couple of days now and I really don't have any idea how.
Just in case there is also 2 Rectangle and Circle classes that I use. They are very similar so I am only putting one.
private double radius;
private double circumference;
private double area;
private ArrayList<Circle> circles;
//throws Exception gives error! learn how to do exceptions!
public Circle(double radius) {
super();
if(radius > 0) {
this.radius = radius;
}
else {
System.out.println("Throw new Exception!!");
}
}
public void setRadius(double radius) throws Exception {
if (radius < 0) {
throw new Exception();
}
else {
this.radius = radius;
}
}
public void setCircumference(double circumference) throws Exception {
if (circumference < 0) {
throw new Exception();
}
else {
this.circumference = circumference;
}
}
public void setArea(double area) {
if (area <= 0) {
System.out.println("Please check your input!");
}
else {
this.area = area;
}
}
public double getRadius() {
return radius;
}
public double getCircumference() throws Exception{
if (radius < 0) {
throw new Exception();
}
else {
return circumference;
}
}
public double getArea() throws Exception{
if (radius < 0) {
throw new Exception();
}
else {
return area;
}
}
#Override
public double circumference() {
return 2 * Math.PI * radius;
}
#Override
public double area() {
return radius * radius * Math.PI;
}
}
The code seems kinda messy but that's because I've been trying everything :) Any advice is welcome. Btw I was using Arraylist but according to some people I know, using Array is much better in this circumstance.
The methods in your Util class are static. A static method should be invoked on the class it is declared as part of. Both of your methods you have declared to take a parameter of type Shape[]. So you should invoke them like so:
Util.getAccumulatedArea(shapes)
…where shapes is a value of type Shape[].
the exception cannot appear, I apparently don't know where is the problem. I want to make own exception for circle radius. For example, if my input is negative value, then the exception need to appear. I made 3 classes. TestCircle.java, Circle.java and IllegalRadiusException.java
TestCircle.java
package circle;
public class TestCircle {
public static void main(String[] args) {
double newRad;
try {
Circle A = new Circle();
A.InputRadius();
A.Calculation();
newRad = A.getRadius();
A.Result();
} catch (IllegalRadiusException e) {
System.out.println(e);
}
}
}
Circle.java
package circle;
import java.util.Scanner;
public class Circle {
Scanner input = new Scanner(System.in);
private double radius;
private double area;
//this is consturctor method
public Circle() throws IllegalRadiusException {
if (radius >= 0) {
this.radius = radius;
this.area = area;
} else {
throw new IllegalRadiusException("Radius Cannot be Negative");
}
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setArea(double area) {
this.area = area;
}
public double getArea() {
return area;
}
//------------------------------------------------------------//
public void Calculation() {
area = 3.142 * radius * radius;
}
public void InputRadius() {
System.out.print("Radius: ");
radius = input.nextDouble();
}
public void Result() {
System.out.println("");
System.out.println("Radius: " + radius);
System.out.println("Area: " + area);
}
}
IllegalRadiusException.java
package circle;
public class IllegalRadiusException extends Exception {
//Extra kena tambah 'extends Exception'
//WAJIB KENA LETAK untuk CREATE OWN EXCEPTION
public IllegalRadiusException() {
super();
}
public IllegalRadiusException(String message) {
super(message);
}
}
In your code, the constructor would be called only once, at the time of object's instantiation.
You've added the check for exception, only in this constructor.
For your example to work as needed, you need to throw exceptions from your InpurRadius method too, if the user enters an invalid value.
Change your method to this.
public void InputRadius() throws IllegalRadiusException {
System.out.print("Radius: ");
double entered_radius = input.nextDouble();
if (entered_radius >= 0) {
this.radius = entered_radius;
this.area = area;
} else {
throw new IllegalRadiusException("Radius Cannot be Negative");
}
}
You do not pass value to radius in constructor so it is not initialized (0). So the if statement will always pass since 0 == 0 :). Pass
public Circle(double radius) // something like this
and assign it inside constructor to radius
I'm trying to write a program that states the color and area of a circle using String color and int radius using sets and gets. Using green & 10 as an example. Here's what I have so far:
public class Circle
{
private String color;
private int radius;
public Circle () {
color = "null";
radius = 0;
}
public Circle (String setColor, int setRadius) {
}
public void setRadius (int radius) {
this.radius = radius;
}
public void setColor(String color) {
this.color = color;
}
public int getRadius(){
return radius;
}
public String getColor(){
return color;
}
public void printInfo(String setColor, int setRadius) {
double area = Math.PI * this.radius * this.radius;
System.out.printf("The " + "%s" + " circle has area " + "%.2f",
this.radius, area);
}
}
public class Main
{
public static void main(String [] args)
{
Circle circle = new Circle("green", 10);
circle.printInfo();
}
}
"circle.printInfo();" has an error and I'm not sure why. Still pretty new to this so any help is appreciated. Thanks!
In the constructor which takes arguments, you forgot to include code which defines the Circle. Right now, you create a circle with undefined variable, even though you passed it information. How to fix:
public Circle (String color, int radius) {
this.setRadius(radius);
this.setColor(color);
}
I believe you thought the arguments in the constructor took the place of the methods you created, but you are creating and defining local variables with the same name as the methods instead.
Hope this helps! Comment on anything that confuses you.
What's up guys,
I keep receiving this error, cannot find symbol Circle aCircle = new Circle(); , when trying to compile the driver code my professor gave us. I'm wondering if it is because I haven't added it to my circle.java method. This is the circle driver.
package lab7;
public class CircleDriver {
public static void main(String[] args) {
Circle aCircle = new Circle();
aCircle.setColor("green");
aCircle.setRadius(10);
aCircle.display();
Double circleArea = aCircle.computeArea();
Double circumference = aCircle.computeCircumference();
System.out.println("circle area: " + circleArea);
System.out.println("circle circumference: " + circumference);
System.out.println();
}
}
This is my circle method.`
public class Circle {
private String color;
private int radius;
public Circle(String color, int radius) {
this.color = color;
this.radius = radius;
}
public Circle() {
Circle aCircle = new Circle();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public void display() {
System.out.println("I am a circle");
System.out.println("My color is " + color);
System.out.println("My radius is " + radius);
}
public double computeArea() {
return (Math.PI * Math.pow(radius, 2));
}
public double computeCircumference() {
return (2 * Math.PI * radius);
}
}
You need to call a super() constructor when calling a circle. When you call
Circle aCircle = new Circle();
You are trying to initialize a circle in the local aspect. I think you are trying to inherit the Circle class that is already in java.
Leaving the circle constructor as
public Circle() {}
Should theoretically work to instantiate your class.
Use this code. You have mistake in constructor. I hope it will solve your issue.
In Circle.java, instead of
public Circle() {
Circle aCircle = new Circle();
}
Use this code
public Circle() {
super();
// TODO Auto-generated constructor stub
}
CircleDriver.java
public class CircleDriver {
public static void main(String[] args) {
Circle aCircle = new Circle();
aCircle.setColor("green");
aCircle.setRadius(10);
aCircle.display();
Double circleArea = aCircle.computeArea();
Double circumference = aCircle.computeCircumference();
System.out.println("circle area: " + circleArea);
System.out.println("circle circumference: " + circumference);
System.out.println();
}
}
Circle.java
public class Circle {
private String color;
private int radius;
public Circle() {
super();
// TODO Auto-generated constructor stub
}
public Circle(String color, int radius) {
super();
this.color = color;
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public void display() {
System.out.println("I am a circle");
System.out.println("My color is " + color);
System.out.println("My radius is " + radius);
}
public double computeArea() {
return (Math.PI * Math.pow(radius, 2));
}
public double computeCircumference() {
return (2 * Math.PI * radius);
}
}
Output:
I am a circle
My color is green
My radius is 10
circle area: 314.1592653589793
circle circumference: 62.83185307179586
This question already has answers here:
Why main() method is needed in java main class
(6 answers)
Closed 8 years ago.
So I'm still new to programming and I don't know if this is all correct or not, but I'm trying to find the area circumference of a circle with a given radius.
So far I have this:
public class Circle {
private double radius;
public Circle(double r) {
}
public double getRadius() {
return radius;
}
public void setRadius(double r) {
}
public double diameter() {
double diameter = radius * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
I also have this other part too...
public class CircleTest {
private static void circleTest (int r) {
Circle circleTest = new Circle(-2);
System.out.printf("Parameter: %d%n", r);
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
public static void main(String[] args) {
}
}
I don't know if this is right or not, but it compiles just fine but it doesn't print anything out when I run it. What am I doing wrong???
the code has few mistakes . it has to be like this
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public void setRadius(double r) {
}
public double diameter() {
double diameter = radius * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
main class has to be like this
public class CircleTest {
public static void main(String[] args) {
Circle circleTest = new Circle(-2);
System.out.printf("Parameter: %d%n", r);
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
}
mistakes you have made 1) your code has to be in main method .2) the constructor parameter has to be set to the class variable.
In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method.
The Java Main Method
You should put some code in this block
public static void main(String[] args) {
}