I am having a problem with abstract classes, the code runs but it won't take any value , it prints "0.0" everywhere, I'm thinking it is an acces problem.I have to make an abstract base class called "Point" where i declare the 3 coordinates of a point, then i have to calculate the area , volume and center point of a cube and a sphere .Also if something doesn't look good, needs formating, or don't understand a word, please tell me.Thank you.
//this is the base class
public abstract class Punct
{
public double x,y,z;
Punct(double x,double y, double z)
{
this.x=x;
this.y=y;
this.z=z;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getZ()
{
return z;
}
}
Then the sphere
//This is the sphere class that extends the Point class
public class Sfera extends Punct
{
private double aria,volumul,raza,centrul;
Sfera(double x, double y, double z,double aria,double volumul,
double centrul,double raza)
{
super(x, y, z);
this.aria=aria;
this.volumul=volumul;
this.centrul=centrul;
this.raza=raza;
}
public double getRaza(Punct p1,Punct p2)
{
raza=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
return raza;
}
public double getAria()
{
aria=4*Math.PI*raza*raza;
return aria;
}
public double getVolumul()
{
volumul=4/3*Math.PI*raza*raza*raza;
return volumul;
}
public double getCentrul(Punct p1)
{
return centrul;
}
}
The Cube
//this is the cube
public class Cub extends Punct
{
double latura,aria,volumul,centrul;
Cub(double x, double y, double z,double latura, double aria,double volumul, double centrul)
{
super(x, y, z);
this.latura=latura;
this.aria=aria;
this.volumul=volumul;
this.centrul=centrul;
}
public double getLatura(Punct p1,Punct p2)
{
latura=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
return latura;
}
public double getAria()
{
aria=6*Math.pow(latura, 2);
return aria;
}
public double getVolumul()
{
volumul=latura*latura*latura;
return volumul;
}
}
And the Test Class where i have the main
//this is the Test class
public class TestTema
{
public static void main(String[] args_)
{
Punct p1=new Punct(2, 2, 2) {};
Sfera obSfera=new Sfera(1,2,3,1,1,1,1);
System.out.println("Raza sfera:" + obSfera.getRaza(p1,p1));
System.out.println("Aria sfera=" + obSfera.getAria());
System.out.println("Volum sfera= " +obSfera.getVolumul());
// System.out.println("Centrul= "+obSfera.getSuprafata());
Cub obCub=new Cub(1,2,3,1,1,1,1);
System.out.println("Lungime latura cub:"+obCub.getLatura(p1,p1));
System.out.println("Aria cubului="+obCub.getAria());
System.out.println("Volumul cubului="+obCub.getVolumul());
// System.out.println("Suprafata="+obCub.getCentrul());
}
}
Your design is really wrong. Sfera has a method getRaza() that computes the distance between two points. There's no reason to use a Sphere to compute the distance betwwen two points. This should be an instance method of Punct, that should take another point as argument.
But there's worse: instead of just computing the distance between two points, it stores this distance in the Sphere, overwriting its previous raza (not sure why a sphere has a distance):
raza=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
You made the same mistake in several other methods: get methods should not change the state of the object.
Now, let's see your code. You start by calling
obSfera.getRaza(p1,p1)
That computes the distance between p1 and itself, so the answer is 0, and this distance is stored in the Sphere (as explained above). So, after this line of code, you've set the sphere's raza to 0. You then execute
obSfera.getAria()
and this method does
aria=4*Math.PI*raza*raza;
return aria;
so, once again, instead of just returning the area of the sphere, it overwrites its area with the computed value, which is 0 since you've set raza to 0 before.
Here's how a sphere class could look like:
public class Sphere {
private final double radius;
public Sphere(double radius) {
this.radius = radius;
}
public double getArea() {
return 4 * Math.PI * this.radius * this.radius;
}
public double getVolume() {
return (4.0 / 3) * Math.PI * this.radius * this.radius * this.radius;
}
}
Key points:
its fields are final: they can't change.
there is no need to store the volume and the area as fields, since they are derived from the radius
getters don't try to modify the object. They just compute a value and return it.
You are using the same point (p1) to calculate raza, thus replacing the value you used in the constructor. And then you used raza to calculate the other values in the Sfera class.
In the Cub class you are doing the same, so the programm is doing what it is supposed to do. If you calculate those things between the point and itself it will give you 0.
As a result the values you used in your constructor where overwritten by your methods.
System.out.println("Raza sfera:" + obSfera.getRaza(p1,p1));
Sorry, I can't translate what is Raza, but this method calculates distance between two points.
You pass into getRaza the same point. Distance between two identical points is zero. That is why getRaza returns zero.
You assign distance between points to internal field Sfera::raza inthe method getRaza
public double getRaza(Punct p1,Punct p2)
{
raza=Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y, 2)+Math.pow(p2.z-p1.z, 2));
return raza;
}
So after calling obSfera.getRaza(p1,p1) the field obSfera.raza equals to zero.
All other methods use obSfera.raza in their formulas:
public double getAria()
{
aria=4*Math.PI*raza*raza;
return aria;
}
public double getVolumul()
{
volumul=4/3*Math.PI*raza*raza*raza;
return volumul;
}
Put raza value into your formulas:
volumul = 4/3*Math.PI*raza*raza*raza = 4/3*Math.PI*0*0*0 = 0
aria = 4*Math.PI*raza*raza = aria=4*Math.PI*0*0
The same is happening with the object obCub
create another object for class Punct and rewrite the code like below:
public class TestTema
{
public static void main(String[] args_)
{
Punct p1=new Punct(2, 2, 2) {};
Punct p2=new Punct(3, 3, 3) {};
Sfera obSfera=new Sfera(1,2,3,1,1,1,1);
System.out.println("Raza sfera:" + obSfera.getRaza(p1,p2));
System.out.println("Aria sfera=" + obSfera.getAria());
System.out.println("Volum sfera= " +obSfera.getVolumul());
// System.out.println("Centrul= "+obSfera.getSuprafata());
Cub obCub=new Cub(1,2,3,1,1,1,1);
System.out.println("Lungime latura cub:"+obCub.getLatura(p1,p2));
System.out.println("Aria cubului="+obCub.getAria());
System.out.println("Volumul cubului="+obCub.getVolumul());
// System.out.println("Suprafata="+obCub.getCentrul());
}
}
classes Cub and Sfera both refering the same object p1, so the distance value Math.pow(p2.x-p1.x,2) always be 0.
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;
}
So I have this question,
Class circle:
Contains an appropriate attribute to store the radius.
Contains a constructor with one parameter to set the radius.
Contains set and get methods.
Contains a method for calculating the area and another method for calculating the
circumference.
Circle should contain an appropriate attribute to keep track (count) of the number of
Circle objects instantiated.
Class TestCircle:
Create an array of 10 circles of radii 1.0 , 2.0, ..., 10.0.
Print the area and circumference of each circle.
Retrieve and print the number of circles that have been instantiated.
My code is:
public class Circle {
public double radius= 0.0;
public int counter;
public Circle (double radius){
this.radius = radius;
counter++;
}
public Circle (){
}
public void setRadius (double radius){
this.radius = radius;
}
public double getRadius (){
return radius;
}
public double Area (){
return 3.14*radius*radius;
}
public double Circumference (){
return 2*3.14*radius;
}
}
public class TestCircle {
public static void main (String args []){
Circle [] arr = new Circle [10];
System.out.println ("The circumference" + arr.Circumference());
System.out.println ("The area" + arr.Area());
System.out.println ("The number of circles" + arr.counter);
}
}
My question is:
How am I supposed to create 10 circle objects with different radius and add it to the array?
I know that the idea was to add the objects to the array by using the for loop but I couldn't add the radius into the process.
Thank you.
Your code could change like
public class Circle {
private double radius;
private static int numberOfCircles = 0;
public Circle (double radius){
this.radius = radius;
numberOfCircles++;
System.out.println("The circumference : " + getCircumference());
System.out.println("The area : " + getArea());
}
public double getRadius (){
return radius;
}
public double getArea (){
return 3.14*radius*radius;
}
public double getCircumference (){
return 2*3.14*radius;
}
public static int getNumberOfCirclesCreated(){
return numberOfCircles;
}
}
public class TestCircle {
public static void main (String args []) {
Circle [] circles = new Circle [10];
for(int counter=0;counter< circles.length;counter++){
circles[counter]=new Circle((double)(counter+1));
}
System.out.println("Number of circles : " + Circle.getNumberOfCirclesCreated());
}
}
Create for loop that iterates through each array cell. You then create a new Circle object and call its setRadius function. You then set array Cell to equal the Circle object and continue iterating till the array is filled.
Additionally, your counter should be static (as corrected by Fildor). Consider also setting radius as private, seeing as you put in getter/setters.
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