Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I came up with 4 classes and several fields, constructors, and methods.

Classes: public class CalculateRectangle, public static class Perimeter extends CalculateRectangle, public static class Area extends CalculateRectangle, public static class Volume extends CalculateRectangle

Fields : private static int length; private static int width; private static int height; private static int p; private static int a; private static int v; private int perimeter; static int p = 0; private int area; static int a = 0; private int volume; static int v = 0;

Constructors: public CalculateRectangle (){ length = 0; width = 0; height = 0; }, public Perimeter () { super(); } public Area () { super(); }public Volume () { super(); }

But I'm having some trouble getting methods to call variables. Would any one tell me what I could do to make it work and still maintain inheritance form?

Java
*/
package calculaterectangle;

/**
 *
 * @author Janie
 */
import java.util.Scanner;

public class CalculateRectangle {

    //class fields
    private static int length;
    private static int width;
    private static int height;
    private static int p;
    private static int a;
    private static int v;
    
    //constructor
    public CalculateRectangle (){
        length = 0;
        width = 0;
        height = 0;               
    }
            
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
        System.out.print("Enter length of rectangle         : ");
        String length = sc.next();
        System.out.print("Enter width of rectangle          : ");
        String width = sc.next();
        System.out.print("Enter height of rectangular prism : ");
        String height = sc.next();
       //method
       p = Perimeter.perimeter.setPerimeter(perimeter);
       a = Area.area.setArea(area);
       v = Volume.volume.setVolume(volume);
       
    System.out.println("perimeter:   " + p + "\n");
    System.out.println("Base Area:   " + a + "\n");
    System.out.println("Volume   :   " + v + "\n");
    
    System.out.print("Continue? (y/n): ");
    choice = sc.nextLine();
    System.out.println();
    }}
      public static class Perimeter extends CalculateRectangle {
         //class fields
         private int perimeter; 
         static int p = 0;
         //constructor
         public Perimeter () {
         super();
         }       
        public class perimeter extends Perimeter {
           //constructor
            public perimeter () {
             super(); }
            //method
           public void setPerimeter(){   
            perimeter = 2 * (length + width);
            p = perimeter;
            }
        }
     }
    
    public static class Area extends CalculateRectangle {
        //class fields
        private int area; 
        static int a = 0;
        //constructor
        public Area () {
        super(); }
        public class area extends Area {
             //constructor 
            public area () {
                super();}
            //method
            public void setArea() {
                area = length * width;
                a = area;
            }
            }
        
        }
  
    public static class Volume extends CalculateRectangle {
        //class fields
        private int volume; 
        static int v = 0;  
        //constructor
        public Volume () {
        super();   }
        public class volume extends Volume {
               //constructor 
              public volume() {
                  super();}
              //method
              public void setVolume() {
                  volume = length * width * height;
                  v = volume;
              }
              }  
        }
       
        
    }
Posted
Updated 30-Jun-12 14:22pm
v3
Comments
[no name] 30-Jun-12 20:10pm    
This is a code dump not a question.
T_Money$ 30-Jun-12 20:25pm    
Sorry I added a more comprehensive question. I have never used anything like this before.
[no name] 30-Jun-12 20:31pm    
You might want to explain what you mean by "getting methods to call variables" since even in java methods are called. You cannot call variables..
T_Money$ 30-Jun-12 20:40pm    
Well I am a newbie here so excuse my grasshopper vernacular:) Basically my Netbeans is saying "non-static variable cannot be referenced from a static contect" and yea...

It is simple, you cannot use a non-static class variable/property from within a static class.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Jul-12 3:21am    
Correct, my 5. I tried to explain it in my answer, not sure it's clear enough for a beginner, maybe some samples would help -- please see.
--SA
In addition to a correct answer by Vitaly:

Instance (non-static) methods are actually exactly the same as the static one, with one additional feature: they have one extra parameter called "this", which is not shown in a parameter list, but can be used inside the implementation of a method. This is a reference to the instance of some object of the type declaring the method. This way, all instance methods deal with some instance of the class, so all non-static members can be use in its implementation, thank to this "this" parameter.

In contrast, static methods has no information about any particular instance, because they are called not with instance but with the type. So, they can access only the members common for the whole class, not specific to any instance, that is, they can only use static data and other static methods, effectively, only the static members.

—SA
 
Share this answer
 
Looking at your question I would suggest a few hours going through the Java Tutorials[^] would be time well spent.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900