Drivers for Classes in Java “Rectangle" - java

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());

Related

Implementing classes that define three shapes in 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.

Inheritance of Square method in java

I have a class Rectangle laid out like this:
package Inheritance;
/**
*
* #author Jacob
*/
public class Rectangle {
final private int length;
final private int width;
public Rectangle (int l, int w)
{
length = l;
width = w;
}
public int getLength ()
{
return length;
}
public int getWidth ()
{
return width;
}
#Override
public String toString ()
{
return String.format ("Rectangle (%dX%d)", length, width);
}
}
I then need to create class square in the following way:
ad Square:
Square extends Rectangle /
No fields are declared in class Square /
It has a parameterized constructor with (only) one parameter /
The parameter is used to initialize both fields of Rectangle /
It has a method called getSide to expose the side-length of the square /
Override the toString method so that it will return a String of the following form: / Square(side) e.g. Square(4)
The values for the sides are going to be hard coded. Rectangle is going to have a width of 4. In order to get the side of the square to be 4 do I create an instance of rectangle and call the method getWidth and set that as the side length. Thats how I would think to do it but in that case I would only be using one of the fields so, My question is how do I initialize both fields? Can I call Rectangle and make length and width equal or is there some other way I should do it?
Here is the code for my Square class:
public class Square {
public Square (int side)
{
super(side, side);
}
public int getSide ()
{
return side;
}
#Override
public String toString ()
{
return String.format ("Square (%d)", side);
}
}
For the line super(side, side) I get the error constructor Object in class Object cannot be applied to given types. Required no arguments, found int, int. For my return statements I get that it cannot find the variable side.
The values for the sides are going to be hard coded.
I assume that you mean that you will hardcode the values for the width and length when you create a Rectangle and Square object (for example in main()). These values should absolutely not be hardcoded any where in the Rectangle and Square classes.
Rectangle is going to have a width of 4. In order to get the side of the square to be 4 do I create an instance of rectangle and call the method getWidth and set that as the side length.
Not at all. Rather, Square should have its own constructor which calls the Rectangle constructor with the same value for both the width and length:
public Square(int side) {
super(side, side); // Set the width and length to the same value.
}

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();
}

Object Construction

I know there is an easy solution to this problem but it's driving me crazy. Why is there an error when I want to print the new Rectangle? any help appreciated!
public class Rectangle {
public Rectangle(int x, int y, int width, int length) {
x = 5;
y = 10;
width = 20;
length = 30;
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.println(new Rectangle());
}
}
There are several problems with your code. First, you may not want to instantiate a Rectangle in the constructor of Rectangle as will lead to infinite recursion. The second problem is that you are calling a constructor that does not exist.
When you write:
new Rectangle()
the Java compiler will look for a constructor in the Rectangle class that accepts no arguments. But your code does not have such a constructor. You can add one like this:
public Rectangle(){
//Your code here to instantiate a default rectangle
}
Usually a constructor is used to set the values of the instance variables in a class rather than to execute code the way you have written it. You can move those lines that are creating rectangles into a main method to test the code.
Here is some code that does what I think you want it to:
public class Rectangle
{
int x, y, width, length; //declares the class's fields
public Rectangle(int x, int y, int width, int length)
{
this.x = x; //initializes the field x to the value of the local variable x
this.y = y; //initializes the field y to the value of the local variable y
this.width = width; //initializes the field width to the value of the local variable width
this.length = length; //initializes the field length to the value of the local variable length
System.out.println(this); //prints this object. should look similar to "Rectangle#1246889"
}
}
Please take a basic java tutorial (e.g. Providing Constructors for Your Classes), it will make your life easier.
You are calling a non-parameterized/default constructor from a parameterized constructor. The JVM in this case unable to create the default constructor. Hence in this case you need to include non-parameterized constructor explicitly into your class.
public class Rectangle {
public Rectangle(int x, int y, int width, int length) {
x = 5;
y = 10;
width = 20;
length = 30;
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.println(new Rectangle());
}
public Rectangle(){}
}
This will be error free.
First, the code (as you have provided it) can not possibly compile: you haven't declared x, y, width and height as member variables (fields) of your Rectangle. E.g.
// I'm assuming you want these private and final (I would)
private final int x, y, width, height;
Alternative, for a quick hack:
int x, y, width, height;
You are also trying to call a 0-argument constructor on your println line. Your class doesn't have a 0-argument constructor; it has a 4-argument constructor. I suspect (as noted above) you really want to print this.
But that wouldn't help much, on its own, unless you add an appropriate toString method to your class. E.g.:
public String toString() {
StringBuilder sb = new StringBuilder("Rectangle: ");
sb.append("x=").append(x);
sb.append(", y=").append(y);
sb.append(", width=").append(width);
sb.append(", height=").append(height);
return sb.toString();
}
You might want to think about implementing equals() and hashCode() too, if you choose to make this class immutable (I would). You can ixquick* or duckduckgo* this - there are plenty of explanations around.
[*] They are search engines: I don't use google.

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.

Categories

Resources