I am so lost I don't even know what question to ask anymore. I have this program that is supposed to be able to construct a Cylinder with parameters radius and height. Then it should be able to call various methods to get and set radius as well as output surface area and volume. I can't get passed go because I cannot put anything in my main without an error about static non static methods being used in static. I don't even know what that means. I actually copy code from others into my compiler and it gives me the same error. Do I have some setting screwed up? I know this is probably too elementary for Stack Overflow but I am desperate at this point.
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public class Cylinder{
private double radius = 0.0;
private double height= 0.0;
private double area = 0.0;
private double volume=0.0;
private String shape = "cylinder";
public Cylinder(double r,double h){
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume(){
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea(){
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius(){
return this.radius;
}
public double getHeight(){
return this.height;
}
public void setHeight(double h){
this.height = h;
}
public void setRadius(double r){
this.radius = r;
}
#Override
public String toString(){
return this.shape + this.radius + this.height+ this.volume + this.area;
}
}
}
Inner classes are just like any other member (well, except for enums). If you don't explicitly declare them static, they won't be, so you won't be able to access them from a static context, such as main. To make a long story short - declare you Cylinder inner class as static, and you should be OK:
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public static class Cylinder{
// etc...
I don't know if you need the outer class but if you just put the main method inside the Cylinder class it compiles for me....
public class Cylinder {
private double radius = 0.0;
private double height = 0.0;
private double area = 0.0;
private double volume = 0.0;
private String shape = "cylinder";
public Cylinder(double r, double h) {
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume() {
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea() {
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius() {
return this.radius;
}
public double getHeight() {
return this.height;
}
public void setHeight(double h) {
this.height = h;
}
public void setRadius(double r) {
this.radius = r;
}
#Override
public String toString() {
return this.shape + this.radius + this.height + this.volume + this.area;
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1, 17);
Cylinder cylinder2 = new Cylinder(3, 8);
Cylinder cylinder3 = new Cylinder(2, 12);
Cylinder cylinder4 = new Cylinder(1, 14);
}
}
Related
I'm getting an error for Sphere.java that says
The type sphere must implement the inherited abstract method GeometricObjects.hit(Ray)
Though I don't see how I am getting this error as I've defined the hit method as shown below.
Below is the Sphere.java code:
package Objects;
import Utility.*;
public class Sphere extends GeometricObjects {
Point3d center;
double radius;
public Sphere(Point3d center, double radius, Color color) {
this.center = new Point3d(center);
this.radius = radius;
this.color = new Color(color);
}
public double hit(Ray ray) {
double a = ray.direction.dot(ray.direction);
double b = 2*ray.origin.sub(center).dot(ray.direction);
double c = ray.origin.sub(center).dot(ray.origin.sub(center)) - radius*radius;
double discriminant = b*b - 4*a*c;
if (discriminant < 0.0){
return 0.0;
}
else {
double t = (-b - Math.sqrt(discriminant)) / (2*a);
if (t > 10E-9) {
return t;
}
else {
return 0.0;
}
}
}
}
This is the GeometricObjects.java code:
package Objects;
import Utility.*;
public abstract class GeometricObjects {
public Color color;
public abstract double hit(Ray ray);
}
And heres the Ray.java code:
package Utility;
public class Ray {
public Point3d origin;
public Vector direction;
public Ray(Point3d origin, Vector direction) {
this.origin = new Point3d(origin);
this.direction = new Vector(direction);
}
}
Practicing abstract classes with shapes. The goal is to get the total area of the 3 shapes using abstract classes. So far this is what I have.
I'm not sure whether I did this part right:
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
return arr.getArea();
}
I keep getting error saying that it doesn't find the symbol h (height), w (width), tw (top_width). Anybody know why it's not finding these symbols?
public class TestShape{
public static void main(String args[]){
Point p = new Point(1, 1);
Shape[] arr = {
new Rectangle(p, 3, 4),
new Parallelogram(p, 5, 6, Math.PI/6.0),
new Trapezoid(p, 5, 6, 2)
};
System.out.println("SUM_AREA = " + sumArea(arr));
}
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
return arr.getArea();
}
}
class Point{
double x, y;
Point(){
this(0, 0);
}
Point(double x, double y){
this.x = x;
this.y = y;
}
public String toString(){
return "[" + x + ", " + y + "]";
}
}
abstract class Shape{
Shape(){
}
Shape(Point p){
this.p = p;
}
public Point getPosition(){
return p;
}
public void setPosition(Point p){
this.p = p;
}
// Abstract method
public abstract double getArea();
}
abstract class Quadrangle extends Shape{
protected double width, height;
Quadrangle(Point p, double w, double h){
this.p = p;
this.width = w;
this.height = h;
}
public double getWidth(){
return w;
}
public double getHeight(){
return h;
}
public void setWidth(double w){
this.weight = w;
}
public void setHeight(double h){
this.height = h;
}
}
class Rectangle extends Quadrangle{
Rectangle(Point p, double w, double h){
this.p = p;
this.width = w;
this.height = h;
}
public boolean isSquare(){
if(w == h){
return "Error";
}
}
#Override /** Return Area */
public double getArea(){
return w * h;
}
}
class Parallelogram extends Quadrangle{
protected double angle;
Parallelogram(Point p, double w, double h, double angle){
this.p = p;
this.weight = w;
this.height = h;
this.angle = angle;
}
public double getAngle(){
return angle;
}
public void setAngle(double a){
this.angle = a;
}
#Override /** Return Area */
public double getArea(){
return w * h;
}
}
class Trapezoid extends Quadrangle{
protected double top_width;
Trapezoid(Point p, double w, double h, double top_width){
this.p = p;
this.width = w;
this.height = h;
this.top_width = top_width;
}
public double getTopWidth(){
return top_width;
}
public void setTopWidth(double tw){
this.top_width = tw;
}
#Override /** Return Area */
public double getArea(){
return ((w + tw) / 2) * h;
}
}
The names w, tw and so on only exist as parameters. When you want to access the values you save in the constructors, you have to use the left hand side name: this.[width or whatever].
Also, rewrite sumArea to something like this:
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
double totalArea = 0;
for (Shape shape : arr) {
totalArea += shape.getArea();
}
return totalArea;
}
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) {
}
I am trying to make class circle extend the Shape class but keep getting a error from JUnit saying the constructor Circle(Point, int) is undefined how would i define the Circle constructor differently from public Circle(Point[] center, int aradius)?
import java.awt.Point;
public abstract class Shape {
private String name;
private Point[] points;
protected Shape(){};
protected Shape(String aName) {
name = aName;
}
public final String getName() {
// TODO Implement method
return name;
}
protected final void setPoints(Point[] thePoints) {
points = thePoints;
}
public final Point[] getPoints() {
// TODO Implement method
return points;
}
public abstract double getPerimeter();
public static double getDistance(Point one, Point two) {
double x = one.getX();
double y = one.getY();
double x2 = two.getX();
double y2 = two.getY();
double x3 = x - x2;
double y3 = y - y2;
double ypow = Math.pow(y3, 2);
double xpow = Math.pow(x3, 2);
double added = xpow + ypow;
double distance = Math.sqrt(added);
return distance;
}
}
Circle.java
import java.awt.Point;
public class Circle extends Shape{
private double radius;
public Circle(Point[] center, int aradius) {
if(radius < 0){
radius = 0;
}
else{
radius = aradius;
}
this.setPoints(center);
}
#Override
public double getPerimeter() {
double perim = 2 * Math.PI * radius;
return perim;
}
public double getRadius(){
return radius;
}
}
Just pass it a single Point, not an array.
public Circle(Point center, int aradius)