• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java programme design help  RSS feed

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

Excuse me if I am posting in the incorrect forum. 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?

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Kusic wrote:
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.


Generally, when a program has many classes, these classes are created using different source files. It is not very common to use inner classes.

Henry
 
George Kusic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wrong

Thank you for your reply. However, when I try to create a new class it still does not work. I cannot run the application from the main file. Please see the code below. Could you please tell me how can I run the programme from the main method which should be contained within 2nd userclass I have created?

Thank you

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, for example you can use this code.


 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're extending cube when you don't have to. If the files are in the same package (usually folder), java can already use them. So just delete the "extends cube" in CubeUser, and your code should work.

Alternatively, you can add a constructor to CubeUser, but there's just no point.


You should be using "extends" if cubeUser is a type of cube, but its not, its just a program that uses cube.

Also one small style tip: the cube class should be capitalized (Cube).
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch, George.

First things first. Congratulations for writing a well stated problem description and for pasting properly formatted code within code tags in your first post.

You've worked out most of what needs to be done; it's just a couple of things that need some more thought.
So your first requirement is-
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 let us start with point 1.
A constructor method which creates a Cube object, the size of which is passed as an argument to the method.


So the Cube has a ( should have a) variable that holds the value of the size. Right? So how do you give this variable to your Cube? Now size is going to be a number, and from your code it looks like you require a double size. So you add a variable to hold the size to the Cube class this way.



Another thing is constructors are not methods and should not be referred to as so. Any book that says they are methods needs editing.
Ok, now going back to point 1, you need a constructor that will construct the Cube object, the size of which will be passed as an argument to the constructor ( don't refer to the constructor as a method; it is wrong to do so despite what some books might say).

You have created your constructor as follows.



Granted while creating an object, you are passing a double size to the constructor, but your Cube is not getting this value because it does not have a variable to hold it. So if you add that one line I suggested above, your code becomes as follows.



Now your cube has a variable that holds the value of the size.
This completes part 1.
Another thing is in Java class names should begin with a capital letter. Small letters are fine as the first letter, cause the compiler allows it. But the standard convention is to use a capital letter as the first letter of your class name. So, now putting everything together your code becomes as follows.



Now we go to part 2.
A method which calculates and returns the cube's total surface area (six times the area of one face).


You have coded this method as follows.



This is fine. But with the changes we have done to the Cube class, the Cube object already knows the size. So we need not ask the user to send it via the method arguments. So let us do away with those arguments. So now our code becomes as follows.



Another thing is method names. We use camelCase style to write method names, i.e the first word has all letters in small letters, we capitalize the first letter of every other word. So with that change, let us change our method name to surfaceArea(). So our code now changes to--




You need change your volume method also accordingly. This would complete your Cube class.

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,


The important words here are "prompt for and read in". So effectively the main method should prompt the user to enter the size of three cubes. I suggest you read about java.util.Scanner class and come with how to prompt the user to provide the size. Let us know if you face any issues.

First, when I try to create a new class called CubeUser within the class Cube

As Henry has already explained, we should create a separate CubeUser class. So how about you do that?

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.

I'm not sure what notification you are getting as you have not copied it here for us to see. If you copy it here, may be we can suggest what parts you need to change and why.

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?

As I have already stated, the requirement says you can't do this.


What you instead need do is ask the user what the sizes are going to be for each cube and then use the values supplied by the user to construct the cube instances. As I've already stated before, you might wanna read about the java.util.Scanner class? Let us know if you found something helpful or if you faced any issues.

Chan.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wrong

Thank you for your reply. However, when I try to create a new class it still does not work. I cannot run the application from the main file. Please see the code below. Could you please tell me how can I run the programme from the main method which should be contained within 2nd userclass I have created?


What do you mean by 'it still does not work'. What command are you typing to run the application? And what error are you getting?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:
Henry Wrong

Thank you for your reply. However, when I try to create a new class it still does not work. I cannot run the application from the main file. Please see the code below. Could you please tell me how can I run the programme from the main method which should be contained within 2nd userclass I have created?


What do you mean by 'it still does not work'. What command are you typing to run the application? And what error are you getting?


His code doesn't compile due to his subclass not having declared a constructor, which is necessary since the superclass did not have the no-param constructor. He can add the appropriate constructors to either cube or CubeUser, but its far more easier and correct to just not extend cube.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!