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
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[].
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
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);
}
}
I need to take information from a file and create them into objects and put them into an array so I can compare the areas of the objects and list in the array which object has the largest area and its location in the array.
I'm confused on how I take the information from the file and create each one into a object (circle or rectangle) and then assign that object into an array after it has been created. I think my other classes are fine, I'm just stuck on finishing the driver.
Normally, I would do something like Circle c1 = new Circle(); to create a new object, but how do I do that from a file with predefined information and assign it to an array?
Data:
“CIRCLE”, 1, “blue”, true
“RECTANGLE”, 1, 2, “blue”, true
“RECTANGLE”, 10, 2, “red”, true
“CIRCLE”, 2, “green”
“RECTANGLE”
“CIRCLE”
Driver:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("C:/Users/Charles/Desktop/GeometricObjectsData.txt"));
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
}
}
}
GeometricObject:
public abstract class GeometricObject {
//class variables
private String color;
private boolean filled;
//constructors
public GeometricObject() {
super();
color = "white";
filled = false;
}
public GeometricObject(String color, boolean filled) {
super();
this.color = color;
this.filled = filled;
}
//mutators
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
//user-defined methods
public abstract double getArea();
public abstract double getPerimeter();
#Override
public String toString() {
return super.toString() + " \tColor=" + this.getColor() + " \tFilled=" + this.isFilled();
}
}
Circle:
public class Circle extends GeometricObject {
//class variables
private double radius;
//constructors
public Circle() {
super();
radius = 1;
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
//mutators
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//user-defined methods
#Override
public double getArea() {
//area of a circle
return (radius * radius * Math.PI);
}
#Override
public double getPerimeter() {
//perimeter of a circle
return (2 * radius * Math.PI);
}
#Override
public String toString() {
return super.toString() + "\nCircle: Radius=" + this.getRadius();
}
}
Rectangle:
public class Rectangle extends GeometricObject {
//class variables
private double height;
private double width;
//constructors
public Rectangle() {
super();
height = 1;
width = 1;
}
public Rectangle(double height, double width, String color, boolean filled) {
super(color,filled);
this.height = height;
this.width = width;
}
//mutators
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//user-defined methods
#Override
public String toString() {
return super.toString() + "\nRectangle: Height=" + this.height + "\tWidth=" + this.width;
}
#Override
public double getArea() {
return (height * width);
}
#Override
public double getPerimeter() {
return (2 * height + 2 * width);
}
}
In your text file, you have special quotes around your shape items. This will make your life more difficult, so you should change that, if possible
Example of how to make objects (from your main method):
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
String[] parts = line.split(",");
if (parts[0].indexOf("Circle") != -1) {
Circle c = new Circle();
// ... parse the rest of the attributes to set up your circle
} else if ... // fill in the other shape cases
}
I am writing a program that has four classes, Circle,Rectangle,GeometricObject and my main one TestGeometricObject.
I dont understand how to read from another file such as notepad using TestGeometricObject?
I dont need help reading classes I just need help at the moment how can I connect what I have to read another file such as one on notepad, not just this program but for any program I were to write? I hope this made sense...let me know if it did not.
public class TestGeometricObject {
public static void main(String[] args) {
GeometricObject geoObject1 = new Circle();
GeometricObject geoObject2 = new Rectangle();
System.out.println("The two objects have the same area? " +
equalArea(geoObject1, geoObject2));
// Display circle
displayGeometricObject(geoObject1);
// Display rectangle
displayGeometricObject(geoObject2);
}
public static boolean equalArea(GeometricObject object1,GeometricObject object2) {
return object1.getArea() == object2.getArea();
}
public static void displayGeometricObject(GeometricObject object) {
System.out.println();
System.out.println("The area is " + object.getArea());
System.out.println("The perimeter is " + object.getPerimeter());
}
}//end main
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}//end
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}//end
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}//end
To read a text file, it is common practice to use a BufferedReader wrapper and FileReader
try {
BufferedReader br = new BufferedReader(new FileReader(new File("yourFile.txt")));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}