Question
My code is complete but I am getting this error cansomeone please help me Main.java:44: error: non-static variable this cannot bereferenced from a static context
My code is complete but I am getting this error cansomeone please help me
Main.java:44: error: non-static variable this cannot bereferenced from a static context
shapes.add(newRectangle(name, length, width));
I believe the project3 class is not recognizing my shapeclasses. I think it's because the classes have to be publicstatic?
Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Project3 {
public static void main(String[] args) {
// If file name is not provided ascommand line argument then print message and
if (args.length < 1) {
System.out.println("Providefile name which contains details about shape");
return;
}
//ArrayList to store shapes
ArrayList
// Get file name from command lineargument
String filename = args[0];
// Declare and initialize fileobject
File file = new File(filename);
try {
// Declare and initializescanner object
Scanner fileScanner = newScanner(file);
fileScanner.useDelimiter("[,]+");
while(fileScanner.hasNextLine()) {
// Read type andname of the shape
String type =fileScanner.next();
String name =fileScanner.next();
//if type is aRectangle
if(type.equalsIgnoreCase("Rectangle")) {
doublewidth = fileScanner.nextDouble();
doublelength = fileScanner.nextDouble();
//Store Shape in an ArrayList.
shapes.add(newRectangle(name, length, width));
}
// if type is aSquare
else if(type.equalsIgnoreCase("Square")) {
doubleside = fileScanner.nextDouble();
//Store Shape in an ArrayList.
shapes.add(newSquare(name, side));
}
// if type is aTriangle
else if(type.equalsIgnoreCase("Triangle")) {
doublebase = fileScanner.nextDouble();
doubleheight = fileScanner.nextDouble();
//Store Shape in an ArrayList.
shapes.add(newTriangle(name, base, height));
}
// if type is aCircle
else if(type.equalsIgnoreCase("Circle")) {
//Read radius as double
doubleradius = fileScanner.nextDouble();
//Store Shape in an ArrayList.
shapes.add(newCircle(name, radius));
}
if(fileScanner.hasNextLine())
fileScanner.nextLine();
}
// close file scannerobject
fileScanner.close();
} catch (FileNotFoundException ex) {
System.out.println("File notfound");
}
// Print shapes in the ArrayList
System.out.println("Shapes in theArrayList are:");
// Iterate through each shape
for (Shape shape : shapes) {
System.out.println(shape);
}
// Print Cumulative Area of shapes
System.out.printf("Cumulative Area ofshapes in the ArrayList is: %.2f",getCumulativeArea(shapes));
// Print Largest shape
System.out.println("Largest shape inthe ArrayList is: " + getLargestShape(shapes));
}
//This method returns a double representing thetotal area of all the shapes
public static doublegetCumulativeArea(ArrayList
double totalArea = 0.0;
// Iterate through each shape in theArrayList
for (Shape shape : shapes) {
totalArea +=shape.getArea();
}
return totalArea;
}
//This method returns the name of the shape whichhas the largest area
public static ShapegetLargestShape(ArrayList
Shape largest = null;
for (Shape shape : shapes) {
// If largest shape is nullor area of current shape is greater than largest
// shape then update largestshape
if (largest == null ||largest.getArea() < shape.getArea())
largest =shape;
}
return largest;
}
public abstract class Shape {
// Attribute for name of the shape
private String name;
public Shape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract double getArea();
}
public static class Rectangle extends Shape {
private double length, width;
public Rectangle(String name, double length, doublewidth) {
super(name);
this.length = length;
this.width = width;
}
//Getters
// return the length
public double getLength() {
return length;
}
//return the width
public double getWidth() {
return width;
}
//Overrided getArea() method
@Override
public double getArea() {
return length * width;
}
@Override
public String toString() {
return String.format("A Rectangle named%s with length %.2f, width %.2f and area %.2f.", getName(), length,width, getArea());
}
}
public static class Square extends Rectangle {
//A parameterized constructor to initialize Squareclass.
public Square(String name, double side) {
super(name, side, side);
}
@Override
public double getArea() {//get method
return getLength() * getLength();
}
@Override
public String toString() {
return String.format("A Square named %swith side %.2f and area %.2f.", getName(), getLength(),getArea());
}
}
public static class Circle extends Shape {
private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
//Getters
//return the radius
public double getRadius() {
return radius;
}
//Overrided getArea() method
@Override
public double getArea() {
return Math.PI*radius*radius;
}
@Override
public String toString() {
return String.format("A Circle named %swith radius %.2f and area %.2f",getName(), radius, getArea());
}
}
public static class Triangle extends Shape {
private double base, height;
public Triangle(String name, double base, doubleheight) {
super(name);
this.base = base;
this.height = height;
}
//Getters
// returns base
public double getBase() {
return base;
}
//returns height
public double getHeight() {
return height;
}
//calculates area of triangle and returns it
@Override
public double getArea() {
return 0.5 * base * height;
}
//prints info for triangle
@Override
public String toString() {
return String.format("A Triangle named%s with base %.2f, height %.2f and area %.2f", getName(), base,height,getArea());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started