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 8 of 8

Thread: Java programme design help

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java programme design help

    Hello guys,

    I am a complete novice in Java. Just started my course in Computer Science and I was given an assignment to write a code for a programme. The test of the exercise is the following:

    Create a Java class called Cube, with the following three methods:

    A constructor method which creates a Cube object, the size of which is passed as an argument to the method.
    A method which calculates and returns the cube's total surface area (six times the area of one face).
    A method which calculates and returns the cube's volume (obtaining by cubing the side length)

    Now create a CubeUser class which contains the main() method. Within this, you should prompt for and read in the sizes of three different cubes, and then instantiate each of these as a Cube object. Then, you should print out the surface area and volume of each cube by calling the appropriate methods.

    I have created something but I ve got two problems. First, when I try to create a new class called CubeUser within the class Cube and then write "public static void main(String argv[])" I am given a notification for an identification error for static method. So I am not sure how/where should I create the second class CubeUser.
    My second issue is whether the programme I have designed is correct. It gives me some results when I test it but specifically I am not sure about the following part of the assignment whether I have done it properly - "you should prompt for and read in the sizes of three different cubes, and then instantiate each of these as a Cube object". Could anyone help me, please?

    --- Update ---

    Ops I forgot to paste the code. It is the following

    public class cube
    {
     
    	public cube(double s)
    	{
    		;
    	}
    	public double surfarea(double oneside)
    	{
    		return (oneside * oneside) * 6;
    	}
     
    	public double volume(double length)
    	{
    		return length * length * length;
    	}
     
     
    		public static void main(String argv[])
    		{
    			cube cb1 = new cube(30);
    			cube cb2 = new cube(36);
    			cube cb3 = new cube(42);
    			System.out.println("the surface area of cub1 is: " + cb1.surfarea(5));
    			System.out.println("the surface area of cub2 is: " + cb2.surfarea(6));
    			System.out.println("the surface area of cub3 is: " + cb3.surfarea(7));
    			System.out.println("the volume of cub1 is: " + cb1.volume(5));
    			System.out.println("the volume of cub2 is: " + cb2.volume(6));
    			System.out.println("the volume of cub3 is: " + cb3.volume(7));
    		}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java programme design help

    Welcome to the forum! Thanks for figuring out how to post your code in code tags.

    Java classes should have names beginning with capital letters.

    As for the correctness of your Cube class, if you had an instance variable, say 'length', you wouldn't need to pass that value to the suraceArea() - should be the name - and volume() methods. To take advantage of that, the constructor would look like:
        public Cube( double length )
        {
            this.length = length;
        }
    This program provides a very simple example of Object Oriented Programming, so it's important to get it right and understand why it's right (or not).

    I don't understand the errors you say you're having. They don't seem to apply to the code you've posted. If that's true, post the code you're having trouble with AND post the exact error message and stack trace, copied and pasted.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java programme design help

    Hello Greg,

    Thank you for your reply. This is how the code should look like, I guess. I am getting the following error in the console - 'cube.java19: Illegal static declaration in inner class cube.CubeUser public static void main(String argv[])
    modified static is only allowed in constant variable declaration'
    1 error.

    Also, I was wondering whether you could provide some comment on the correctness of my code. Thank you very much

    --- Update ---

    public class cube
    {
     
    	public cube(double s)
    	{
    		;
    	}
    	public double surfarea(double oneside)
    	{
    		return (oneside * oneside) * 6;
    	}
     
    	public double volume(double length)
    	{
    		return length * length * length;
    	}
    		class CubeUser
    	{
    		public static void main(String argv[])
    		{
    			cube cb1 = new cube(30);
    			cube cb2 = new cube(36);
    			cube cb3 = new cube(42);
    			System.out.println("the surface area of cub1 is: " + cb1.surfarea(5));
    			System.out.println("the surface area of cub2 is: " + cb2.surfarea(6));
    			System.out.println("the surface area of cub3 is: " + cb3.surfarea(7));
    			System.out.println("the volume of cub1 is: " + cb1.volume(5));
    			System.out.println("the volume of cub2 is: " + cb2.volume(6));
    			System.out.println("the volume of cub3 is: " + cb3.volume(7));
    		}
     
    		}
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java programme design help

    What's the purpose of the inner class CubeUser? Why do you need/want it?

    Edit: Oh, oh, oh. I see. You're trying to follow the directions. You don't create an inner class CubeUser. Instead, you create another class CubeUser in its own file. Does that make sense?

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java programme design help

    Hello Greg,

    Thanks for your reply but it's still does not work. I have created a new source file and typed in

    	}
    		class CubeUser
    	{
    		public static void main(String argv[])
    		{
    			cube cb1 = new cube(30);
    			cube cb2 = new cube(36);
    			cube cb3 = new cube(42);
    			System.out.println("the surface area of cub1 is: " + cb1.surfarea(5));
    			System.out.println("the surface area of cub2 is: " + cb2.surfarea(6));
    			System.out.println("the surface area of cub3 is: " + cb3.surfarea(7));
    			System.out.println("the volume of cub1 is: " + cb1.volume(5));
    			System.out.println("the volume of cub2 is: " + cb2.volume(6));
    			System.out.println("the volume of cub3 is: " + cb3.volume(7));
    		}
     
    		}
    }

    But then it tells me that it cannot find the values and variables. Any ideas on how should I do it?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java programme design help

    You posted the same code. You didn't even try to do what I suggested.

    Oh well. There's more than one way to skin this cat. One is to include both classes in the same file, but file naming is important, and your IDE or development environment may complain. For example, the following works just fine in Eclipse as the one file is called "CubeUser.java":
    public class CubeUser
    {
        public static void main(String argv[])
        {
            Cube cb1 = new Cube(30);
            Cube cb2 = new Cube(36);
            Cube cb3 = new Cube(42);
            System.out.println( "the surface area of cub1 is: " + cb1.surfarea() );
            System.out.println( "the surface area of cub2 is: " + cb2.surfarea() );
            System.out.println( "the surface area of cub3 is: " + cb3.surfarea() );
            System.out.println( "the volume of cub1 is: " + cb1.volume() );
            System.out.println( "the volume of cub2 is: " + cb2.volume() );
            System.out.println( "the volume of cub3 is: " + cb3.volume() );
        }
     
    }
     
    class Cube
    {
        private double length;
     
        public Cube( double length )
        {
            this.length = length;
        }
     
        public double surfarea()
        {
            return ( length * length ) * 6;
        }
     
        public double volume()
        {
            return length * length * length;
        }
     
    }
    If you are required to save the classes in two separate files, CubeUser.java and Cube.java, then you should figure out how to do that. It's not hard.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java programme design help

    Thank you Greg. The code is more or less the same except the changes you've made. I was just wondering why swaping the places of the classes within the same file rectified the issue. Is it because the static class which I use to run the program through the main method should be the top one and the Cube method must be a subclass of it? Sorry for these questions. I am still a complete novice. Thank you

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java programme design help

    This is basic Java. The top-level public class named the same as the file name and including a main() method will be the entry point for the program. The main() method buried in a nested class of a different name than the top-level public class and not the same as the file name will not have the same results. In fact, I'm not sure there's any point to a design like that, but I'd have to investigate further.

Similar Threads

  1. i am new to java plzz help to remove error in this programme
    By akshay1991 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 28th, 2012, 09:00 AM
  2. Java programme to colour in a grid
    By why_always_me in forum Java Theory & Questions
    Replies: 20
    Last Post: March 29th, 2012, 06:09 AM
  3. How to call a java programme from an applet?
    By prativa in forum Java Applets
    Replies: 2
    Last Post: December 19th, 2011, 11:01 AM
  4. Java jar programme with a microcontroller
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 11th, 2011, 08:01 AM
  5. access cisco router with java programme
    By vigneswara in forum Java Theory & Questions
    Replies: 1
    Last Post: May 11th, 2010, 01:36 AM