Implementing classes that define three shapes in Java - java

I think I've implemented everything asked within these set of instructions:
Design and implement a set of three classes that define shapes: RoundShape, Sphere, Cone. For each class, store fundamental data about its size and provide methods to access and modify this data. In addition, provide appropriate methods to compute the area, and volume, for Sphere and Cone. In your design, consider how shapes are related and thus where inheritance can be implemented. Don't create duplicate instance variables. Create a main method which instantiates 2 Sphere objects (any parameters), 2 Cone objects (any parameters), display them with ToString(), change one parameter (your choice) in each, and display them again.
Here is my code:
class RoundShape{
double shape = 9;
double radius = 4;
int cone1 = 3;
int sphere1;
public String toString(){
return " the man" + cone1 + "this also" + sphere1;
}
}
//--------------------------------------------------------------
// class Sphere that extends RoundShape
//--------------------------------------------------------------
class Sphere extends RoundShape{
double getArea(){
double area = 4 * Math.PI * Math.pow(radius, 2);
return area;
} // end of getArea
double getVolume(){
double volume = (4/3) * Math.PI * Math.pow(radius, 3);
return volume;
} // end of getVolume
} // end of the class Sphere
//---------------------------------------------------------------
// class Cone that extends RoundShape
//---------------------------------------------------------------
class Cone extends RoundShape{
double height = 8;
double getArea(){
double area = Math.PI * radius * (radius + Math.sqrt(Math.pow(height, 2) + Math.pow(radius, 2)));
return area;
} // end of getArea for Cone
double getVolume(){
double volume = Math.PI * Math.pow(radius, 2) * (height/3);
return volume;
} // end of getVolume for Cone
} // end of the class Cone
public class Murray_A03A4 {
public static void main(String[] args) {
Sphere sphere1 = new Sphere();
sphere1.getArea();
sphere1.getVolume();
System.out.println(sphere1);
Cone cone1 = new Cone();
cone1.getArea();
cone1.getVolume();
System.out.println(cone1);
} // End of class header
} // End of method header
My main question is, how do I refer back to the toString method from content thats in the main method? Additionally, is the toString found within the correct class or should I place it in a new class or should I create a toString for every class?
Thanks for your help!

implement toString() methods in both Sphere & Cone. In those toString methods, put details specific to those classes and for the fields of the superclass invoke super.toString()
For Cone, it would be like :
public String toString() {
return height + super.toString();
}

Use super allows you to access parent class and interfaces.
Also, feel free to use casting:
var self = (BaseOfBase)super;
self.VirtualMethod();
Turned up a lil bit with csharps' base which is completely same.

I'm not quite sure what your question is but what I think you're trying to ask for is: "How to call the toString method on Sphere objects and Cone objects when in the main method they are simply RoundObject?"
You should Override the toString method in every class, and it should return the information relevant to that class. So the RoundObject does not need return the volume, just the radius (maybe something like return "RoundShape with radius " + radius;). Then you would do the same for sphere and cone, but it would also include the shapes volume.
Then in main, you can simply call the toString method on the RoundObjects. Because the toString method is an instance method (doesn't have the static keyword in its title) it is dynamically bound. Meaning, the method from the actual underlying object will be used.
I see that you are trying to pull sphere/cone fields (sphere1, cone1) into RoundObject but this is not required. (it's actually better for a parent to not know anything about child classes)
for more information, look up polymorphism, or this video.

Related

OOP - Accessing Objects Via Reference Variables with Instance Classes

I am learning Classes and Objects, and I am on reference variables, and accessing an object's data and methods. In my textbook, we created a program which calculates the area of a circle, given the radius.
They declare the object reference variable, create an object, and assign the reference to a variable here:
Circle myCircle = new Circle();
They later give an example below of finding the area (getArea()* just returns the area given the radius):
System.out.println("Area is " + new Circle(5).getArea());
Is the 5 (number in parentheses) an input for the radius?
If so, why isn't it in the getArea() parentheses?
Also, there are no arguments for Circle() so how can you have a number in the () anyway?
*Code for getArea():
By the way, could you get rid of the parentheses if there is only one statement inside?
double getArea()
{
return radius * radius * Math.PI;
}
Please excuse the horrid formatting - I wasn't able to use Ctrl-K, could someone edit it for me please.
Is the 5 (number in parentheses) an input for the radius?
System.out.println("Area is " + new Circle(5).getArea());
Not exactly.
It is the argument passed to a Circle constructor that should very probably value a radius field.
If so, why isn't it in the getArea() parentheses?
getArea() is an instance method of Circle. It relies on the state of the Circle instance that has already a radius information.
So passing a radius argument to getArea() makes no sense.
It would make sense if you had a utility static method in the Circle class to compute a area according to a radius.
For example :
public static double computeArea(double circleRadius){
...
}
You could invoke it in this way :
double area = Cicle.getArea(5.5);
Also, there are no arguments for Circle() so how can you have a number
in the () anyway?
Without a Circle constructor that accepts a radius information, the invocation new Circle(5) cannot compile. So it of course requires one.
You should have a Circle constructor defined such as :
public class Circle{
...
private double radius;
public Circle(double radius){
this.radius = radius;
}
}
by the way, could you get rid of the parentheses if there is only one
statement inside?
Parenthesis () refers to a specification of the Java language to declare method and constructor.
When you declare a method, you need it in any case.
I will try to explain in deep:
public class Circle {
// this variable can access only inside class
private final double radius;
// constructor for class Circle
public Circle(double rad) {
radius = rad;
}
// method of class Circle - can be access outside
public double area() {
return radius*radius*Math.PI;
}
}
when you instantiate class Circle as Circle circle5 = new Circle(5); you got instance of class Circle with encapsulated parameter radius = 5 now, you don't know any details, and just call method double area = circle5.area() to get the area of circle (rad*rad*Pi).
This is dramatically decrease complexity. Even in this very simple example.
now you can print you result
System.out.println(area);
I think, you got the idea.
1st step: you instantiated class and encapsulated all details into it via constructor;
2nd step: you can use you class (methods of the class) and never aware about implementation detail.
And in you sample, calling constructor without parameters new Circle() instead new Circle(5) - this is typographic mistake.
First let me explain constructors in Java.
There are two types of constructors in java: 1. Explicit and 2. Implicit.
Explicit means a constructor with argument and developed by developer.
Implicit means, default or non parameter constructor.
In your case let me analyse the operation:
System.out.println("Area is " + new Circle(5).getArea());
new Circle(5), actually means that invoking a n explicit constructor with input parameter 5.
According to your shared knowledge, it can be understood that a radius is set in this explicit constructor.
I mean, the following constructor should be exist:
public Circle(int i) {
radius = i;
}

Drivers for Classes in Java “Rectangle"

I’ve never used a separate file for a driver in Java. I’m used to just using a main method. I’ve used separate files in Python but Java is new. Below is my code for each class (“Rectangle” and “Driver”), each from separate files.
Update with the methods changed to static: Don’t pay attention to the change in class names or formatting…I’m just tweaking so it will work with MyProgrammingLab. I still have to add in parameters for length and width being between 0.0 and 20.0 only (easy if-else statements).
import java.util.Scanner;
public class Driver{
public static void main(String[] args) {
Scanner input = new Scanner( System.in);
System.out.print("Enter length of rectangle:");
double length = input.nextDouble();
System.out.print("Enter width of rectangle:");
double width = input.nextDouble();
Rectangle Perimeter = new Rectangle(length, width);
Perimeter.getPerimeter();
Rectangle Area = new Rectangle(length, width);
Area.getArea();
System.out.printf("Area: %.1f, Perimeter: %.1f",Rectangle.getArea(),Rectangle.getPerimeter());
}
}
final class Rectangle {
private static double mLength;
private static double mWidth;
public Rectangle(double length, double width){
mLength = length;
mWidth = width;
}
public double getLength(){
return mLength;
}
public double getWidth(){
return mWidth;
}
public static double getArea(){
double area = mWidth*mLength;
return area;
}
public static double getPerimeter(){
double perimeter = (mWidth*2)+(mLength*2);
return perimeter;
}
}
It makes more sense to create a Rectangle object with it's length & width, so use your overloaded Rectangle constructor by passing the length and width arguments (entered by user) as shown below:
Rectangle Perimeter = new Rectangle(length, width);
the constructor Rectangle() is undefined. Can anyone help?
The important point is that when you have an overloaded constructor like in your Rectangle class (where there are no default i.e., no argument constructors written), you can't create an object using new Rectangle();, this is because compiler doesn't add the default constrcutor automatically for you. I suggest look here for more details on this.
Also, if you wanted to print the Rectangle object with length & width details, you need to override toString() method from java.lang.Object method as shown below:
public class Rectangle {
private double mLength;
private double mWidth;
//add your code here as is
#Override
public String toString() {
return "Rectangle [mLength=" + mLength + ", mWidth=" + mWidth + "]";
}
}
The default constructor is provided by compiler if there are no constructor written explicitly.
But if you explicitly write any constructor in the class, then whenever you call a constructor, be it no-argument or with arguments, it will always look for explicitly defined constructor in class.
And, this is logically correct since, if you want to block creation of objects without any data in it, adding a constructor with argiment is the way to go.
So either explicitly write a no argument constructor in Rectangle and use setter to set its attributs, or just use the argument constructor in your method.
Add to Rectangle.class an empty constructor :
public Rectangle() {
}
Or Use constructor declared with parameters in your method
Rectangle rectangle = new Rectangle(length, width);
In your case you are using the rectangle object wrong.
I think what you looking to do is this :
Rectangle rectangle = new Rectangle(length , width );
System.out.printf("Area: %.1f, Perimeter: %.1f",rectangle.getArea() ,rectangle.getPerimeter());

Declaring a private instance variable and referencing it in a different class [duplicate]

This question already has answers here:
Java - access private instance variables
(6 answers)
Closed 9 years ago.
I'm not really sure how to word the title nor this but I'll try to be as specific as I can. I have created a class that contains a constructor that allows me to create objects of "Circle" type, and this circle has an argument radius. Here is that class:
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius; // this.radius refers to the instance variable above
}
public Circle() {
}
public double getArea() {
double area = radius * radius * Math.PI;
return area;
}
public double getCircumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
I think the methods contained in this code are straight forward. I create an object given a radius, and it computes the circumference and area.
I also have another class, CircleTester, where I have created 2 objects of Circle type, c1 and c2, with a radius of 5 and 10, respectively.
public class CircleTester {
public static void main (String[] args) {
Circle c1 = new Circle(5);
double circumference1 = c1.getCircumference();
double area1 = c1.getArea();
System.out.println("Radius of c1: " + c1.radius);
System.out.printf("The circle c1 with circumference %.3f has area %.3f%n", circumference1, area1);
Circle c2 = new Circle(10);
double area2 = c2.getArea(); //objectReference.methodName()
double circumference2 = c2.getCircumference(); //objectReference.methodName()
System.out.println("Radius of c2: " + c2.radius);
System.out.printf("The circle c2 with circumference %.3f has area %.3f%n", circumference2, area2);
}
}
I'm having errors with the following 2 lines:
System.out.println("Radius of c2: " + c2.radius);
System.out.println("Radius of c2: " + c2.radius);
The program is failing to recognize c2.radius as the instance variable "radius" is declared as private in the circle class.
My question is is there any way to allow the CircleTester program to read the values of the radius of each object, without changing the instance variable from private to public? c1.radius and c2.radius do not work - the error is "Circle.radius is not visible" (because it is private)**
The reason I don't want to make it public is because I've been told by my tutor that declaring an instance variable as public can be seen as bad programming, and we could possibly be penalized in the exam.
I'm sorry if this is vague or stupid - explaining has never been my strong point. I'm also fairly new to Java so I am not 100% sure if I'm using all my terms correctly.
Just make a getter method:
public double getRadius() {
return radius;
}
The simplest (and standard) way is to declare a "getter" method in the Circle class, i.e.
public double getRadius() {
return radius;
}
Alternatively, you could use reflection, but it is not meant to be used for cases like the one described in the question. So, a getter method is the solution.
The best way is to create a getter for you variable radius in your class Circle
public double getRadius(){
return radius;
}
and use
System.out.println("Radius of c2: " + c2.getRadius());
When you define an object, consider all the things you might like to ask of the object. For example, for a circle object you may want to ask the area, radius, diameter, circumference ... so
Don't get seduced into exposing the internal parameters of an object directly.
public class Circle {
private double radius;
public Circle(Double radius) { this.radius = radius;}
public double getArea() { return radius*radius*Math.PI; }
public double getRadius() { return radius;}
public double getDiameter() { return 2*radius;}
public double getCircumference() { return Math.PI*getDiameter();}
}
Just use getter to get the variable:
public double getRadius() {
return radius;
}
This is in fact a good programming practice. You could do a setter similarly if you wanted to alter this value.
While a getter method is the best way to go, here's an approach using reflection. I wouldn't recommend using this unless you want your Circle object to be effectively immutable.
Circle circle = new Circle(10.0);
Field radius;
try {
radius = circle.getClass().getDeclaredField("radius");
radius.setAccessible(true); // required since field is private
System.out.println(radius.getDouble(circle));
radius.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}

Writing class method for test class

public class CirclTest{
public static void main(String[] args){
Circle first=new Circle('R',3.0);
Circle first=new Circle('R',3.0);
Circle second=new Circle();
System.out.println("first's radius is " + first.getRadius());
System.out.println("first's area is " + first.getArea());
System.out.println("second's area is " + second.getArea());
if(first.hasLargerAreaThan(20)){
System.out.println("first's area is larger than 20. ");
}else{
System.out.println("first's area is smaller than 20. ");
}
}
}
So i am supposed to write a circle class.This is what i have done.
public class Circle{
private double radius=0.0;
private double area=0.0;
private char colour=' ';
public Circle(char colour,double radius){
this.colour=colour;
this.radius=radius;
}
public Circle(){
radius=0;
colour='B';
}
public char getColour(){
return colour;
}
public double getRadius(){
return radius;
}
public double getArea(){
return area;
}
}
I am actually confused on how to write a class.Like i know i need to initialize the variables by private etc.And i need to build a constructor but somehow this code above does not work.the test method is correct.But i have to use it to implement my class.
You're declaring the variable
Circle first
twice. If you want to reassign its value, just do
first=new Circle('R',3.0);
And inside the if statement you're calling
first.hasLargerAreaThan(20)
when I don't see such a method defined in your class.
Can you please what you mean by the code does not work? If you are referring to area not getting calculated correct and is always 0, that is happening because you have a default value for the same as 0 and are never calculating it. You might want to put calculation logic in getArea() method.
First, you're going to want to use a testing framework to assert the validity of your code, if that's what is required. Look into JUnit.
A sample assertion of if the area was larger than some value would be written like this.
#Test
public void assertArea_calculatedProperly() {
//given that the radius is 5,
Circle c = new Circle('R', 5);
//when I get the area...
double result = c.getArea();
//then I expect it to be around 78.53981634.
assertTrue(result < 78.6);
assertTrue(result > 78.5);
}
Second, your getArea isn't actually getting the area. There's nothing in your code to retrieve, then calculate the area. You're not even using Math.PI. I would recommend that you implement that - but use the unit test as a valid way to assert that you're going to get an appropriate response back.

Adding up total in a class file

I'm pretty new to OOP, so any help is appreciated. I have a class file that takes some inputs and calculates the area of a triangle like such:
public class Triangle
{
private int base;
private int height;
private double area;
private String name;
private double totalArea = 0;
public void writeOutput()
{
System.out.println("Triangle name: " + name);
System.out.println("Triangle base: " + base);
System.out.println("Triangle height: " + height);
System.out.println("Triangle area: " + setArea());
System.out.println("Total Area: " + totalArea);
}
private double setArea()
{
area = (base * height) * 0.5;
return area;
}
private double setTotalArea()
{
totalArea += area;
return totalArea;
}
}
My problem is in the setTotalArea() method. Basically, it is supposed to add up a cumulative total of all the areas calculated, however, all it is doing is echoing the area and I can't figure out why. Any suggestions are appreciated!
You're mixing up setters and getters. For instance, your setArea() method is not a true "setter" method as it is private, accepts no parameter, sets nothing, and tries to return a value -- the exact opposite of most things a setter should do, and the same goes for your setTotalArea.
Anyway, sorry to say, but this code is borked, and I recommend you simply get rid of it and start over (it's not that big of a class yet, so this is no big deal), but start with your fields (height, base, and name), the constructor, perhaps one that sets class properties and with true public setter methods that actually accept parameters and use those parameters to set class fields. Then create your getter methods that return the values of the class fields. Your class shouldn't even have a setArea method or an area field since the area is a calculated value, not a set value, ant it should probably be calculated on the spot in the getArea() method and returned.
Edit, you state:
My problem is in the setTotalArea() method. Basically, it is supposed to add up a cumulative total of all the areas calculated, however, all it is doing is echoing the area and I can't figure out why. Any suggestions are appreciated!
Sorry, but I don't understand this. total of what areas? The class only describes one triangle and so there is no cumulative area concept here. Now if you have another class that holds an array or collection of Triangle objects, then a *get*TotalArea (not set total area) method would make sense as it would add the areas of all triangles in the collection, but you don't have this here.
I am not very clear to your execution but if -
Triangle t1 = new Triangle(); t1.setArea();
Triangle t2 = new Triangle(); t2.setArea();
Above 2 are seperate Objects , If you are using the above kind of Object creation , then you can not expect to get cumulative total as both object have their own copy of setArea method.

Categories

Resources