Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help me please with Shape hierarchy

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me please with Shape hierarchy

    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 }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me please with Shape hierarchy

    non-static variable this cannot be referenced from a static context
    The non static inner classes can only be created in an instance of their enclosing class.
    It should compile if the classes are changed to static by adding the keyword static before the keyword class:
    static class Circle ...
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with Shape hierarchy

    Quote Originally Posted by Norm View Post
    The non static inner classes can only be created in an instance of their enclosing class.
    It should compile if the classes are changed to static by adding the keyword static before the keyword class:
    static class Circle ...
    That does work but then I get this error when I do this:

    Driver.java:96: error: no enclosing instance of type Driver is in scope
    public Circle(double r) {this.r = r;}
    ^
    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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me please with Shape hierarchy

    no enclosing instance of type Driver is in scope
    Make sure all inner classes are static.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with Shape hierarchy

    Quote Originally Posted by Norm View Post
    Make sure all inner classes are static.

    Please edit your post and wrap your code with code tags:
    import java.util.Scanner;
     
    class Driver
    {
     
     
    	 private static void main(String[] args)
    	{
    		Object obj;
    		int first;
    		int second;
    		double r;
    		Scanner stdin = new Scanner(System.in);
    		System.out.print("Enter\n1)Two dimensional shape\n2)Three dimensional shape:");
    		first = stdin.nextInt();
    		if(first==1)
     
    		{	
    		System.out.print("Enter\n1)Circle\n2)Square\n3)Triangle:");
    		second = stdin.nextInt();
     
    	     } else {
    		System.out.print("Enter\n1)Sphere\n2)Cube\n3)Tetrahedron:");
    		second = stdin.nextInt();
    	     }
    	     int choice = first*10 + second;
    	     switch(choice)
    	     {
    	     case 11: 
    	    	 System.out.print("Enter radius of circle:");
    	    	 r = stdin.nextDouble();
    	    	 obj = new Circle(r);
    	    	 System.out.print(obj.toString());
    	    	 break;
    	     case 12:
    	    	 System.out.print("Enter side of square:");
    	    	 r = stdin.nextDouble();
    	    	 obj = new Square(r);
    	    	 System.out.print(obj.toString());
    	    	 break;
    	     case 13:
    	    	 System.out.print("Enter side of triangle:");
    	    	 double side1 = stdin.nextDouble();
    	    	 System.out.print("Enter side of triangle:");
    	    	 double side2 = stdin.nextDouble();
    	    	 System.out.print("Enter side of triangle:");
    	    	 double side3 = stdin.nextDouble();
    	    	 obj = new Triangle(side1, side2, side3);
    	    	 System.out.print(obj.toString());
    	    	 break;
    	     case 21:
    	    	 System.out.print("Enter side of sphere");
    	    	 r = stdin.nextDouble();
    	    	 obj = new Sphere(r);
    	    	 System.out.print(obj.toString());
    	    	 break;
    	     case 22:
    	    	 System.out.print("Enter side of cube:");
    	    	 r = stdin.nextDouble();
    	    	 obj = new Cube(r);
    	    	 System.out.print(obj.toString());
    	    	 break;
    	     case 23:
    	    	 System.out.print("Enter side of tetrahedron:");
    	    	 r = stdin.nextDouble();
    	    	 obj = new Tetrahedron(r);
    	    	 System.out.print(obj.toString());
    	    	 break;
    	    	default:
    	    		System.out.print("NOPE");
    	    		break;
     
     
     
     
     
     
     
    	     }
     
    }
     
    static abstract class Shape extends Object {}
    static abstract class TwoDimensionalShape extends Shape
    {
    	public double perimeter () {return 0.0;}
    	public double area() {return 0.0;}
    	public String toString()
    	{
    		return String.format("Area: %.2f%nPerimeter: %.2f%n", area(), perimeter ());
    	}
    }
    static class Circle extends TwoDimensionalShape
    {
    	private double r;
    	public Circle(double r) {this.r = r;}
    	public double area() {return Math.PI*this.r*this.r;}
    	public double perimeter() {return Math.PI * 2 * this.r;}
     
    }
     
    static class Square extends TwoDimensionalShape
    {
    	private double side;
    	public Square(double side) {this.side = side;}
    	public double area() {return this.side * this.side;}
    	public double perimeter() {return this.side * 4;}
    }
     
    static class Triangle extends TwoDimensionalShape
    {
    	private double side1;
    	private double side2;
    	private double side3;
    	public Triangle(double side1, double side2, double side3)
    	{
    		this.side1 = side1;
    		this.side2 = side2;
    		this.side3 = side3;
    	}
    	public double perimeter() {return this.side1 + this.side2 + this.side3;}
    	public double area() 
    	{
    		double half = (this.side1+this.side2+this.side3)/2;
    		double calc = Math.sqrt(half*(half-this.side1)*(half-this.side2)*(half-this.side3));
    		return calc;
    	}
    }
     
    static class ThreeDimensionalShape extends Shape
    {
    	public double getVolume() {return 0.0;}
    	public double getSurfaceArea() {return 0.0;}
    	public String toString()
    	{
    		return String.format("Surface area:%.2f%nVolume: %.2f%n", getSurfaceArea(), getVolume());
    	}
     
     
    }
     
    static class Sphere extends ThreeDimensionalShape
    {
    	private double r;
    	public Sphere(double r) {this.r = r;}
    	public double getVolume() {return (Math.PI * this.r * this.r * this.r * 4)/3.0;}
    	public double getSurfaceArea() {return 4*Math.PI * this.r * this.r;}
     
    }
     
    static class Cube extends ThreeDimensionalShape
    {
    	private double side;
    	public Cube(double side) {this.side = side;}
    	public double getVolume() {return this.side * this.side * this.side;}
    	public double getSurfaceArea() {return this.side*this.side*6;}
     
    }
     
    static class Tetrahedron extends ThreeDimensionalShape
    {
    	private double side;
    	public Tetrahedron(double side) {this.side = side;}
    	public double getVolume() {return this.side * this.side * this.side/(Math.sqrt(2)*6);}
    	public double getSurfaceArea() {return this.side * this.side * Math.sqrt(3);}
    }
    }

    to get highlighting and preserve formatting.[/QUOTE]

    I put the code inside but when I add the static to the classes I am getting this error:

    Problems Detected:
    ⇒ The contents of your standard output is incorrect.

    Given the following was entered from the keyboard:
    1
    2
    2.68

    you displayed:

    instead of:
    Enter
    1)Two dimensional shape
    2)Three dimensional shape:Enter
    1)Circle
    2)Square
    3)Triangle:Enter side of square:Area: 7.18
    Perimeter: 10.72


    ⇒ Failed 12 out of 12 test runs.

    Failed Test Run #2

    ⇒ The contents of your standard output is incorrect.
    ⇒ There is an error in your prompts.
    Last edited by Norm; June 24th, 2019 at 08:54 AM. Reason: Removed noparse tags

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me please with Shape hierarchy

    The contents of your standard output is incorrect.
    What program does that message come from?

    you displayed:

    instead of:
    Can you follow that program's suggestions to change your code to satisfy that program?

    Also try executing the program using the java command so you can see what the code does when its executed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with Shape hierarchy

    Quote Originally Posted by Norm View Post
    What program does that message come from?


    Can you follow that program's suggestions to change your code to satisfy that program?

    Also try executing the program using the java command so you can see what the code does when its executed.
    I changed it from private static void to public static void and the myprpgramminglab is at least prompting

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me please with Shape hierarchy

    Do you have any more questions?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with Shape hierarchy

    Sorry I thought I responded, I figured it out, thank you so much for your help Norm!

Similar Threads

  1. Help designing a class hierarchy for a Java program
    By zmsras in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2014, 03:31 AM
  2. PLEASE HELP ME OUT Parent-Child hierarchy construction
    By _disha_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 22nd, 2013, 11:31 AM
  3. Inheritance & Hierarchy Exercises?
    By Beacon in forum Object Oriented Programming
    Replies: 2
    Last Post: September 21st, 2012, 11:34 AM
  4. class hierarchy
    By jcarosella10 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 9th, 2012, 09:08 AM
  5. Class hierarchy... giving me a headache, so I could use some help
    By Kerr in forum Object Oriented Programming
    Replies: 6
    Last Post: May 30th, 2011, 05:03 PM