I am having trouble with this code for my Java class, I can not get this to work in the Myprogramming lab, it seemed to be an issue with the prompt. Here is the damage I have done thus far and thanks!:
Driver.java:32: error: non-static variable this cannot be referenced from a static context
obj = new Circle(r);
^
Driver.java:38: error: non-static variable this cannot be referenced from a static context
obj = new Square(r);
^
Driver.java:48: error: non-static variable this cannot be referenced from a static context
obj = new Triangle(side1, side2, side3);
^
Driver.java:54: error: non-static variable this cannot be referenced from a static context
obj = new Sphere(r);
^
Driver.java:60: error: non-static variable this cannot be referenced from a static context
obj = new Cube(r);
^
Driver.java:66: error: non-static variable this cannot be referenced from a static context
obj = new Tetrahedron(r);
^
6 errors
1 import java.util.Scanner;
2
3 class Driver
4 {
5
6
7 private static void main(String[] args)
8 {
9 Object obj;
10 int first;
11 int second;
12 double r;
13 Scanner stdin = new Scanner(System.in);
14 System.out.print("Enter\n1)Two dimensional shape\n2)Three dimensional shape:");
15 first = stdin.nextInt();
16 if(first==1)
17
18 {
19 System.out.print("Enter\n1)Circle\n2)Square\n3)Tri angle:");
20 second = stdin.nextInt();
21
22 } else {
23 System.out.print("Enter\n1)Sphere\n2)Cube\n3)Tetra hedron:");
24 second = stdin.nextInt();
25 }
26 int choice = first*10 + second;
27 switch(choice)
28 {
29 case 11:
30 System.out.print("Enter radius of circle:");
31 r = stdin.nextDouble();
32 obj = new Circle(r);
33 System.out.print(obj.toString());
34 break;
35 case 12:
36 System.out.print("Enter side of square:");
37 r = stdin.nextDouble();
38 obj = new Square(r);
39 System.out.print(obj.toString());
40 break;
41 case 13:
42 System.out.print("Enter side of triangle:");
43 double side1 = stdin.nextDouble();
44 System.out.print("Enter side of triangle:");
45 double side2 = stdin.nextDouble();
46 System.out.print("Enter side of triangle:");
47 double side3 = stdin.nextDouble();
48 obj = new Triangle(side1, side2, side3);
49 System.out.print(obj.toString());
50 break;
51 case 21:
52 System.out.print("Enter side of sphere");
53 r = stdin.nextDouble();
54 obj = new Sphere(r);
55 System.out.print(obj.toString());
56 break;
57 case 22:
58 System.out.print("Enter side of cube:");
59 r = stdin.nextDouble();
60 obj = new Cube(r);
61 System.out.print(obj.toString());
62 break;
63 case 23:
64 System.out.print("Enter side of tetrahedron:");
65 r = stdin.nextDouble();
66 obj = new Tetrahedron(r);
67 System.out.print(obj.toString());
68 break;
69 default:
70 System.out.print("NOPE");
71 break;
72
73
74
75
76
77
78
79 }
80
81 }
82
83 abstract class Shape extends Object {}
84 abstract class TwoDimensionalShape extends Shape
85 {
86 public double perimeter () {return 0.0;}
87 public double area() {return 0.0;}
88 public String toString()
89 {
90 return String.format("Area: %.2f%nPerimeter: %.2f%n", area(), perimeter ());
91 }
92 }
93 class Circle extends TwoDimensionalShape
94 {
95 private double r;
96 public Circle(double r) {this.r = r;}
97 public double area() {return Math.PI*this.r*this.r;}
98 public double perimeter() {return Math.PI * 2 * this.r;}
99
100 }
101
102 class Square extends TwoDimensionalShape
103 {
104 private double side;
105 public Square(double side) {this.side = side;}
106 public double area() {return this.side * this.side;}
107 public double perimeter() {return this.side * 4;}
108 }
109
110 class Triangle extends TwoDimensionalShape
111 {
112 private double side1;
113 private double side2;
114 private double side3;
115 public Triangle(double side1, double side2, double side3)
116 {
117 this.side1 = side1;
118 this.side2 = side2;
119 this.side3 = side3;
120 }
121 public double perimeter() {return this.side1 + this.side2 + this.side3;}
122 public double area()
123 {
124 double half = (this.side1+this.side2+this.side3)/2;
125 double calc = Math.sqrt(half*(half-this.side1)*(half-this.side2)*(half-this.side3));
126 return calc;
127 }
128 }
129
130 class ThreeDimensionalShape extends Shape
131 {
132 public double getVolume() {return 0.0;}
133 public double getSurfaceArea() {return 0.0;}
134 public String toString()
135 {
136 return String.format("Surface area:%.2f%nVolume: %.2f%n", getSurfaceArea(), getVolume());
137 }
138
139
140 }
141
142 class Sphere extends ThreeDimensionalShape
143 {
144 private double r;
145 public Sphere(double r) {this.r = r;}
146 public double getVolume() {return (Math.PI * this.r * this.r * this.r * 4)/3.0;}
147 public double getSurfaceArea() {return 4*Math.PI * this.r * this.r;}
148
149 }
150
151 class Cube extends ThreeDimensionalShape
152 {
153 private double side;
154 public Cube(double side) {this.side = side;}
155 public double getVolume() {return this.side * this.side * this.side;}
156 public double getSurfaceArea() {return this.side*this.side*6;}
157
158 }
159
160 class Tetrahedron extends ThreeDimensionalShape
161 {
162 private double side;
163 public Tetrahedron(double side) {this.side = side;}
164 public double getVolume() {return this.side * this.side * this.side/(Math.sqrt(2)*6);}
165 public double getSurfaceArea() {return this.side * this.side * Math.sqrt(3);}
166 }
167 }