The question is to create objects of both sub classes and store them in an array .
So I create a abstract Super class and made a method area abstract after that I created the two sub classes and implemented that method on the main method I declared array and given the values this is it. I am new here so sorry if I'm asking it in wrong way.
And yes the output should be the area and types of two figure.
package Geometric;
public abstract class GeometricFigure {
int height;
int width;
String type;
int area;
public GeometricFigure(int height, int width) {
//super();
this.height = height;
this.width = width;
}
public abstract int area();
}
package Geometric;
public class Square extends GeometricFigure {
public Square(int height, int width) {
super(height,width);
}
public int area(){
return height * width;
}
}
package Geometric;
public class Triangle extends GeometricFigure {
public Triangle(int height, int width) {
super(height ,width);
}
public int area() {
return (height*width)/2;
}
}
package Geometric;
public class UseGeometric {
public static void main(String args[]) {
GeometricFigure[] usegeometric = { new Square(12, 15), new Triangle(21, 18) };
for (int i = 0; i < usegeometric.length; i++) {
System.out.println(usegeometric[i]);
usegeometric[i].area();
System.out.println();
}
}
}
You already are storing both elements in an array, I think your question is more related to this part:
usegeometric[i].area();
System.out.println();
You get the area of both elements, but you don't assign it to a variable, and you don't do anything with it. Change those lines of code to this:
System.out.println("Area: " + usegeometric[i].area());
EDIT:
Geometric.Square#19dfb72a Geometric.Triangle#17f6480
This is the kind of output you can expect because you didn't overwrite the toString method in your classes.
If you don't, it will take the inherited version of Object, which prints this information
--
In your Square class, add this:
public String toString() {
return "Square - area = " + area();
}
or something similar, depending on what you want to be printed. (And a similar adjustment to your Triangle class).
At this time, you are printing Object's version of toString, since you didn't provide a new one. By overwriting that method, you should get the output you want after turning your loop into:
for (int i = 0; i < usegeometric.length; i++) {
System.out.println(usegeometric[i]);
}
What println actually does, is not print the object itself, but a String representation of the object, which is provided by the toString method.
public class UseGeometric {
public static void main(String[] args) {
// TODO Auto-generated method stub
GeometricFigure[] usegeometric = { new Square(12, 15), new Triangle(21, 18) };
for (int i = 0; i < usegeometric.length; i++) {
System.out.println("Area is: " + usegeometric[i].area());
}
} }
Related
I'm working on a tiny exercise java program that calculates circle and square (classes) area, that implements surface (interface) which has a method called area(). A requirement is that I have to implement a class called SumArea that has a generic method called calcArea() that receives Circle circ[] and Square square[] arrays and executes area calculation.
Program structure:
-> UseSumArea.java (main method)
-> Surface.java (interface)
-> Square.java (class that implements Surface.java)
-> Circle.java (class that implements Surface.java)
-> SumArea.java (class that executes calcArea() method)
UseSumArea.java
public class UseSumArea {
public static void main(String[] args) {
Square square[] = { new Square(2.0), new Square(5.0) };
Circle circ[] = { new Circle(3.0), new Circle(2.0) };
Surface surf[] = new Surface[square.length + circ.length];
surf[0] = square[0];
surf[1] = square[1];
surf[2] = circ[0];
surf[3] = circ[1];
SumArea sum = new SumArea();
System.out.println("Square's sum area = " + sum.calcArea(square));
System.out.println("Circle's sum area = " + sum.calcArea(circ));
System.out.println("Surface's sum area = " + sum.calcArea(surf));
}
}
Surface.java
public interface Surface {
public double area();
}
Square.java
public class Square implements Surface {
private double area;
private double side;
public Square(double l) {
this.side = l;
area();
}
#Override
public double area() {
return this.area = (this.side)*(this.side);
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
}
Circle.java
public class Circle implements Surface {
private double area;
private double radius;
public Circle (double r) {
this.radius = r;
area();
}
#Override
public double area() {
return area = (((this.radius)*(this.radius))*(Math.PI));
}
public double getRadius() {
return radius;
}
public void setRadius(double raio) {
this.raio = raio;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
SumArea.java
public class SumArea {
private double area;
public <T> double calcArea(T[] t) { //generic method that receives Square and Circle arrays
double arrayArea = 0;
for (T a : t) {
arrayArea = arrayArea+(a.area());
}
return this.area = arrayArea;
}
}
My doubt is over this SumArea's code snippet:
arrayArea= arrayArea+(a.area());
How can I access the area() method of each Circle and Square objects inside this generic method?
You need to bound the type variable:
public <T extends Surface> double calcArea(T[] t) {
or just declare the parameter as an array of Surfaces:
public double calcArea(Surface[] t) {
Note that the latter is preferable because generics and arrays don't play very nicely together. If you were to need to have a type variable for other reasons, it would be advisable to change to a Collection, or similar:
public <T extends Surface> double calcArea(Collection<T> t) {
(And, as a minor matter of preference, I would use S rather than T to name a type variable which extends Surface)
Since the problem in regard to generic types is already addressed by Andy Turner, I just want to add a suggestion related to the class design.
I think there is a bit of redundancy in how these classes were designed. You need to create an instance of SumArea in order to do the calculation. And the result of the last of the last calcArea() method call will be stored in this object (let's assume that this calculation is far more complex and CPU-consuming).
But do we really need to store somewhere else the value is already returned by the method? In this case, the idea to cash the history of calculations (as a single variable or as a collection of values) doesn't seem to be useful because it can't be reused without knowing which objects were involved in the calculation.
And without storing the result this method will not be bound to a state, i.e. it has to be static. And since interfaces can have static methods, instead of creating a utility class for that purpose it could be placed in the Surface interface. Like that.
public interface Surface {
public double area();
public static <T extends Surface> double calcArea(T[] t) { // generic method that receives Square and Circle arrays
double arrayArea = 0;
for (T a : t) {
arrayArea += a.area();
}
return arrayArea;
}
}
Note that static behavior declared in interfaces in contrast to classes could be invoked only by using the name of an interface:
System.out.println("Circle's sum area = " + Surface.calcArea(circ));
Also note that it makes sense for both classes to have a field area inside the classes Circle and Square only if other fields will be declared as final, i.e. they must be initialed only one during the object construction and setters become unnecessary.
In this case (assuming that radius has been declared as final and is being validated when assigned so that reduce > 0) method area() will look like this:
#Override
public double area() {
if (area > 0) { // `0` is a default value for instance variables
return area; // reusing already calculated value
}
return area = radius * radius * Math.PI;
}
And there mustn't be two methods area() and getArea() leave either one or another.
I excepted that my functions in Level.java class allow me to make 2D array copy at any time in my program then change size of level array and fill it with values of copy and at last display it.
When I try to run my program it shows NullPointerException at line 24 in Level.java (a part which replaces the values).
Game.java Class
package main;
public class Game {
public static void main(String[] args) {
Level lvl = new Level();
char[][] exampleLevelTemplate = new char[][] {
{'#','#','#'},
{'#','#','#'},
{'#','#','#'}
};
lvl.setLevelSize(3,3);
lvl.setLevelLayout(exampleLevelTemplate);
lvl.displayLevel();
}
}
Level.java Class
package main;
public class Level {
private int levelWidth;
private int levelHight;
private char[][]level;
public void setLevelSize(int Width,int Height)
{
levelWidth = Width;
levelHight = Height;
char[][]level = new char[levelWidth][levelHight];
}
public void setLevelLayout(char[][]levelTemplate)
{
int a;
int b;
for(a=0; a < levelWidth; a++)
{
for(b=0; b<levelHight; b++)
{
level[a][b] = levelTemplate[a][b]; //Error happens here
}
}
}
public void displayLevel()
{
int a;
int b;
for(a=0; a < levelWidth; a++)
{
for(b=0; b<levelHight; b++)
{
System.out.println(level[a][b]);
}
}
}
}
Change your setLevelSize method to this:
public void setLevelSize(int Width,int Height)
{
levelWidth = Width;
levelHight = Height;
level = new char[levelWidth][levelHight];
}
You will see that line:
char[][]level = new char[levelWidth][levelHight];
was changed to:
level = new char[levelWidth][levelHight];
You need to just assign the array Object to array reference variable "level" and not create a local one and initialize it like you did.
You have been assigning value to a null array reference variable and therefore got NullPointerException.
I was wondering if I could get some help with my length and width. I dont know how to get them into the format of being a string. I thought about the toString() idea, but then I think I would need a char value for that. Any help would be amazing.
public class Rectangle
{
// instance variables
private int length;
private int width;
/**
* Constructor for objects of class rectangle
*/
public Rectangle(int l, int w)
{
// initialise instance variables
length = l;
width = w;
}
// return the height
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
public String String()
{
return System.out.println(length + " X " + width);
}
}
I have changed your String() method to toString() which I overrided. This method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object.You can check this
public class Rectangle
{
// instance variables
private int length;
private int width;
/**
* Constructor for objects of class rectangle
*/
public Rectangle(int l, int w)
{
// initialise instance variables
length = l;
width = w;
}
// return the height
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
#Override
public String toString()
{
// TODO Auto-generated method stub
return length + " X " + width;
}
}
class Main{
public static void main(String[] args) {
Rectangle test = new Rectangle(3, 4);
System.out.println(test.toString());
}
}
Rename String() method to toString() (the method that normally returns an object string representation) and return from it length + " X " + width.
You can use String as a method name, but it violates JCC and looks abnormally.
Methods should be verbs, in mixed case with the first letter
lowercase, with the first letter of each internal word capitalized.
Examples:
run();
runFast();
getBackground();
Try this.
public class Rectangle {
// instance variables
private int length;
private int width;
/**
* Constructor for objects of class rectangle
*/
public Rectangle(int l, int w)
{
// initialise instance variables
length = l;
width = w;
}
// return the height
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
#Override
public String toString()
{
return length + " X " + width;
}
public static void main(String[] args) {
Rectangle rec = new Rectangle(8, 9);
System.out.println(rec.toString());
}
}
So I have been pulling my hair out trying to understand how to call methods on objects in an array. The gist of this homework is to create a comparable interface from scratch, an abstract class called "Weapon", an interface called "Drawable", a couple classes that extend Weapon, and a "Spaceship" class that creates a spaceship with an array of weapon objects.
The following is the code for the "Spaceship" class. The error "not a statement" occures at line 40. The intended purpose of the fireFastestWeapon method is to sort the object array based on each objects fire time, and then activate the fire method for the first n weapons.
public class Spaceship implements Drawable
{
private Weapon [] mountedWeapons = new weapon[4];
private int curWeapon = 0;
public void draw()
{
System.out.print("Ship will be drawn here");
}
public void addWeapon(Weapon w)
{
if (curWeapon<mountedWeapons.length)
{
mountedWeapons [curWeapon] = w;
curWeapon++;
}
else System.out.print("The weapons bay is full Commander");
}
public void fireFastestWeapon(int n)
{
int count=mountedWeapons.length;
int k;
for (int m = count; m>=0;m--)
{
for(int i = 0; i<count-1;i++)
{
k=i+1;
if (mountedWeapons[i].compareTo(mountedWeapons[k]) == 1){
Weapon temp;
temp = mountedWeapons[i];
mountedWeapons[i] = mountedWeapons[k];
mountedWeapons[k] = temp;
}
}
}
if(n>mountedWeapons.length)
{
n=mountedWeapons.length;
}
for (f=0;f<n-1;n++)
{
mountedWeapon[f].fire;
}
}
}
Thank you for looking!
The parentheses on a method call are not optional in Java:
mountedWeapon[f].fire();
I'm trying to understand Java by trying several things out. I'm using two classes in the same package. One is called Box, the other one is called TestBox. I want to calculate the area of the company box using calculateArea(). This function is in another class TestBox. However the function calculateArea in Box does not respond to the function in TestBox. I'm missing a link between these two classes. This seems like a simple problem, but I have not found the solution yet. Can someone please help me out?
package box;
public class Box {
int length;
int width;
public static void main(String[] args) {
Box company = new Box();
company.length = 3;
company.width = 4;
int area = company.calculateArea();
}
}
package box;
public class TestBox {
int length;
int width;
int calculateArea(){
int area = length * width;
System.out.println("Area= " + area);
return area;
}
}
I think you have to clarify a bit what is your design, I mean what the classes Box and TestBox should do, moreover I advice use to use an IDE such as Eclipse or Intellij Idea helping you with syntax highlight and founding possible errors.
What you are dealing with is the encapsulation, that is
packing of data and functions into a single component.
so it is feasible that the area of the box is calculated by the Box class itself.
About your code, a possible solution could be:
package com.foo;
public class Main {
public static void main(String[] args) {
Box box = new Box(3, 4);
int area = box.calculateArea();
System.out.println("Box area is: " + area);
}
}
class Box {
private int l;
private int w;
Box(int length, int width) {
l = length;
w = width;
}
int calculateArea() {
return l * w;
}
}
Another possible approach could be
package com.foo;
public class Main {
public static void main(String[] args) {
Box box = new Box(3, 4);
TestBox testBox = new TestBox();
int area = testBox.calculateArea(box);
System.out.println("Box area is: " + area);
}
}
class Box {
private int l;
private int w;
Box(int length, int width) {
l = length;
w = width;
}
public int getLength() {
return l;
}
public int getWidth() {
return w;
}
}
class TestBox {
int calculateArea(Box box) {
return box.getLength() * box.getWidth();
}
If you want to have a separate class doing the job, but it is something I do not like, the function computing the area is related to the box and works on box variables, I prefer the first one, but it should be better to have more details in case.
I hope it helps.
As per the below approach, you need to make parameterized constructor where you can pass the value of length and width at the time of creating TestBox object. You also need to create a default constructor for future save in case if later you only want to create default TestBox object like new TestBox();
You can calculate area in you TestBox class and than return the value in the Box class. You don't need to create separate length and width variable in Box class and no need to create a object of Box.
package box;
public class Box {
public static void main(String[] args) {
TestBox company = new TestBox (3,4);
int area = company.calculateArea();
}
}
package box;
public class TestBox {
private int length;
private int width;
TestBox()
{
}
TestBox(int l, int w)
{
this.length = l;
this.width = w;
}
public int getLength() {
return length;
}
public int getWidth() {
return width;
}
int calculateArea(){
int area = length * width;
System.out.println("Area= " + area);
return area;
}
}
public static void main(String[] args) {
TestBox company = new TestBox();
company.length = 3;
company.width = 4;
int area = company.calculateArea();
}
}