I'm supposed to find the volume of a cylinder using a Circle object I made in another class. When I create my getVolume method, it tells me I can't multiply a Circle and double, and wanted to know how to fix it. I can't make a getArea method in the Cylinder class, just make a new Circle using a user-inputted radius. Here's the code (first for the Circle class, and second the Cylinder class):
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
public class Cylinder {
private Circle base;
private double height;
public Cylinder(double r, double h) {
base = new Circle(r);
height = h;
}
public double getVolume() {
return base * height;
}
}
So the getVolume method is my problem. How can I get the program to recognize "base" as a double while it is still a Circle object?
You wanted to write
public double getVolume() {
return base.getArea() * height;
}
Right?
Otherwise, just by thinking of it: do you multiply a circle with a length? No, you multiply an area with a length to get the volume...
Also, if the circle would have a name attribute too, what should be multiplied? There is no magic, the JVM does what you tell it to do.
You need to multiply the area of the circle by the height. But you can't multiply a Circle and a double. Call getArea() on your Circle.
return base.getArea() * height;
return base.getArea() * height
Related
I created an interface 'Polygon' that stores the abstract methods: 'area' and 'perimeter'. However, I am not understanding how to effectively use the interface, when the classes that implement Polygon have different computations involved for calculating area and perimeter. In my opinion, I don't even need an interface 'Polygon' since it has no use in my code.
I've tried overriding the method 'area' in the Triangle class, but received the following error:
Triangle is not abstract and does not override abstract method area() in Polygon
since the Triangle area has constructors. I cannot modify the Polygon area method to have the same number of constructors needed for Triangle, because it will not then suit my Rectangle class.
public interface Polygon {
void area();
void perimeter();
}
class Triangle implements Polygon{
private double triangleArea;
private double trianglePerimeter;
public Triangle (){};
public void area(){}; //I've tried overriding method here but get a
compiler error since it is not identical to the Polygon method.
public double area(double base, double height){
triangleArea = base * height * (.5);
return triangleArea;
}
public class Project25 {
public static void main(String[] args) {
Triangle testTriangle = new Triangle();
testTriangle.area(2, 2);
testTriangle.printArea();
I've managed to obtain the answers I need in my code i.e. area and perimeter, but I need to know how to modify my code to utilize inheritance and polymorphism.
The purpose of an interface is to be an abstract view of the common features of the objects.
In case of polygons, you've already identified some things they have on common, i.e. all polygons have an area, and they all have a perimeter.
The abstract view would be to get those common values:
public interface Polygon {
double getArea();
double getPerimeter();
}
Of course, you can only get those values if the polygon is fully defined, e.g. for a triangle the base and the height might be enough to calculate the area, but not the perimeter. Instead, you usually use the lengths of the 3 sides.
public class Triangle implements Polygon {
private final double a;
private final double b;
private final double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
#Override
public double getArea() {
// Using Heron's Formula
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
#Override
public double getPerimeter() {
return a + b + c;
}
}
A rectangle is even easier.
public class Rectangle implements Polygon {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
#Override
public double getArea() {
return width * height;
}
#Override
public double getPerimeter() {
return (width + height) * 2;
}
}
As you can see, since the inputs for calculating area are different, depending on the type of polygon, the area method cannot take a common set of parameters, therefore they must be embedded in the polygon object, so we can get a common area method.
I think,
Methods in Polygon interface should return values (area and perimeter).
Each shape class that implements Polygon should have its own fields and appropriate constructor to initialize them. (length and height for example), which will be used for implementation of area and perimeter.
So,
Class Triangle should have a constructor,
public Triangle (final Double base, final Double height) {
this.base = base;
this.height = height;
};
And your area method should be like,
#Override
public Double area() {
return base * height * (0.5d);
}
And you should use it something like below,
final Polygon polygon = new Triangle(13, 212);
final Double area = polygon.area();
final Double perimeter = polygon.perimeter();
The first problem I see is that you didn't declare the perimeter method in the class that implements the interface, so that would cause a compiler error right there. As to what others said, you're returning a value on both of those methods, so they shouldn't be void methods.
Also, why did you use an overloaded method in the Triangle class?
The problem I was given:
Write an abstract superclass encapsulating a shape: a shape has 2 abstract methods: one returning the perimeter of the shape, another returning the area of the shoe. It also has a constant field named PI. This class has two non-abstract subclasses: one encapsulating a circle, and the other encapsulating a rectangle. A circle has one additional attribute, its radius. A rectangle has 2 additional attributes, its width and height. You also need to include a client class to test these two classes.
Here's the work I've done:
Shape.java
public abstract class Shape
{
public abstract double getPerimeter();
public abstract double getArea();
}
Circle.java
public class Circle extends Shape
{
private double radius;
final double pi = Math.PI;
//Defualt Constructor, calls Shape default constructor
public Circle()
{
//Set default value to radius
this.radius = 1;
}
public Circle(double radius)
{
this.radius = radius;
}
public double getArea()
{
//Return πr^2 (area formula)
//Use Math.pow method (page 141) in order to calculate exponent
return (pi * Math.pow(radius, 2));
}
public double getPerimeter()
{
//Return 2πr (perimeter formula)
return (2 * pi * radius);
}}
Rectangle.java
public class Rectangle extends Shape
{
private double width, height;
public Rectangle()
{
//set default value to width and height
this.width = 1;
this.height = 1;
}
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return 2 * (width + height);
}}
ShapeClient.java
public class ShapeClient {
public static void main(String [] args)
{
// To test Rectangle...
double width = 13, length = 9;
Shape rectangle = new Rectangle(width, length);
System.out.println("The rectangle width is: " + width
+ " and the length is: " + length
+ "The area is: " + rectangle.getArea()
+ "and the perimeter is: " + rectangle.getPerimeter() + ".");
//To test Circle...
double radius = 3;
Shape circle = new Circle(radius);
System.out.println("The radius of the circle is: " + radius
+ "The area is: " + circle.getArea()
+ "and the perimeter is: " + circle.getPerimeter() + ".");
}}
My question is: Does the constant field for PI need to be in the Shape class rather than the Circle class? If so, how should I about taking it out of the circle class and how should I place it in the Shape class?
The abstract class should only contain fields & methods that are general to all shapes such as getArea and getPerimeter.
In this case PI is only specific to the Circle shape or to rephrase, the square has no use for the constant PI. PI should therefore only reside in the 'Circle' class and not the Shape class.
The PI attribute definitely needs to be on the Circle class. The abstract Shape class should contain attributes and methods that all of its sub-classes are going to use or implement. In this case, the Rectangle class has no need for the PI attribute.
just move the constant to the abstract class.
I have a home work in a java... I tried many ways, but, apparently, stackoverflow is a last :(
So, I have this class:
public final class Span {
private final double length;
public Span(){
this(0);
}
public Span(double length) {
this.length= length;
}
public double getLength(){
return length;
}
}
public final class Circle {
private Span radius;
//radius with double
public Circle(double radius) {
}
// span
public Circle(Span radius){
this.radius= radius;
}
//calculate area.....
public double area(double radius){
return Math.PI * (radius * radius);
}
But:
**private Span radius**; <---- This is necessary :(
I have to create Span(type) radius (in Circle class), Span diameter and Span perimeter.
public final class Circle {
private Span radius;
//radius with double
public Circle(double radius) {
this.radius = new Span(radius);
}
// span
public Circle(Span radius){
this.radius= radius;
}
//calculate area.....
public double area(){
return Math.PI * (radius.getLength() * radius.getLength());
}
It's hard to see much value in the Span class over and above a raw double. I can think of two features you could add that would make it more useful:
Negative lengths make no sense. I'd add a check in the Span constructor to the throw an IllegalArgumentException if a length less than zero is passed it prior to assignment.
Encapsulating units along with the length (cm, m, inches, feet, etc.) would be an improvement.
Having those, you just need to be able to calculate using spans:
double perimeter = 2.0*Math.PI*Span.getLength();
You should not be setting radius once you construct the Circle. Let it tell you what the area is using the original Span.
I'm currently undertaking a Java class (one of my final ones for my bachelor, yay) and I'm having a really difficult time trying to understand classes and do this problem below. The textbook I'm currently using is quite confusing and I've tried to use other online resources to figure out what I'm doing wrong but I still seem stuck on the question below. Whenever I try and run the program all I get is 0.00.0 for my answer, is this due to myself incorrectly assigning values to cylinder1? Also, for the toString() class how do I even go about doing this? I'm always getting errors on converting doubles to Strings no matter what I can do.
Any help would be appreciated it.
Thanks.
Prompt
Implement the class called Cylinder shown in UML below. The constructor accepts and initializes the radius and height for the Cylinder, while accessors and mutators allow them to be changed after object construction. The class also include methods that calculate and return the volume and surface area of the Cylinder. Lastly, it contains a toString method that returns the name of the shape, its radius, and its height. Create a main method which instantiates 4 Cylinder objects (any parameters), display them with toString(), change one parameter (your choice) in each, and display them again. [15 points]
UML
Code
import java.util.*;
import java.text.*;
import java.io.*;
import java.lang.*;
class Cylinder
{
private double radius, height, area, volume;
public Cylinder(double height, double radius) {
radius = 0.0;
height = 0.0;
}
public double getRadius() {
return radius;
}
public double getHeight() {
return height;
}
public double getArea() {
double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
return area;
}
public void setRadius(double r) {
radius = r;
}
public void setHeight(double h) {
height = h;
}
public double calcVolume() {
double volume = Math.PI * Math.pow(radius, 2) * height;
return volume;
}
public String toString (){
StringBuilder StBuild = new StringBuilder();
StBuild.append(radius).append(height);
return StBuild.toString();
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(5, 5);
System.out.println(cylinder1);
}
}
Since this is obviously homework I won't give you the answers, but I'll try to explain a few things.
This:
public Cylinder(double height, double radius) {
radius = 0.0;
height = 0.0;
}
is a constructor. When you create an object (and instance of a class) you call this. You call it by doing:
Cylinder cylinder1 = new Cylinder(5, 5);
But what happens in your class? When you call the constructor are you really saving the values you want?
As for the toString method, you could either call the toString for the double (height.toString) or you could just do what I always end up doing which is just cheat by adding a string to it.
public String toString (){
return "Cylinder [ h: " + height + " - r: " + radius + " - v: " + calcValume() + "]";
}
in class Cylinder change the constructor to:
public Cylinder(double height, double radius) {
this.radius = radius;
this.height = height;
}
In void main() :
Cylinder cylinder1 = new Cylinder(5, 5);
System.out.println(cylinder1.calcVolume());
This will work.
But you should shift the main method to some other class.
In the constructor you are setting the radius and height to 0.0. Try:
public Cylinder(double height, double radius) {
this.radius = radius.
this.height = height;
}
I have to write a program using constructors which calculates the area of a circle using 5 methods:
Circle: The constructor that creates a circle with radius = 1
setRadius: takes an double argument and sets the radius to the argument
getRadius: returns an double argument with the value of the radius
computeDiameter: calculates the diameter and returns the value of the diameter
computeArea: calculates the area and returns the value of the area
So far, I reached here..
Main Class:
class MyClass{
public static void main(String[] args) {
MyClass1 circle= new MyClass1();
System.out.println(circle.computeArea());
}
}
This is the second class.. I haven't named it Circle though..
public class MyClass1 {
private double radius;
private double diameter;
private double area;
public MyClass1(){
radius= 1.0;
}
public void setRadius(double radius){
this.radius= radius;
}
public double getRadius(){
return radius;
}
public double computeDiameter(){
diameter= 2.0*radius;
return diameter;
}
public double computeArea(){
area= (Math.PI* Math.pow(diameter, 2))/4;
return area;
}
The problem is that the output for the area is giving me 0.0
your diamter is initially 0 and only gets set to the correct value after calling computeDiameter() , so try to replace
area= (Math.PI* Math.pow(diameter, 2))/4;
with area= (Math.PI* Math.pow(computeDiameter(), 2))/4;
In constructor:
public MyClass1(){
radius= 1.0;
}
diameter was not initialized. So it has value 0 set by default.
Your method:
public double computeArea(){
area= (Math.PI* Math.pow(diameter, 2))/4;
return area;
}
uses diameter parameter but it is zero at the moment it is used.
Well you have not given diameter a value, so the diameter is 0.