Results 1 to 11 of 11
  1. #1
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Java problem question

    Hello I have given this task from my teacher.

    Make a constructor that computes the area, the circumference and the diameter of a circle. You have to input three values as strings in the compiler and use them to make the computation of the circle. (x,y,radius).

    I tried to write a program but is not very good i think !!! When i try to run the program the compiler gives me some strange messages.

    the program

    Java Code:
    public class Circleinput {
    
    
    		
    			private double num[0],num[1],num[2];//Instance variables  
    			// The constructor method
    			public Circleinput(double x,double y ,double z) {
    				this.num[0] = x;
    				this.num[1] = y;
    				this.num[2] = z;
    			}
    			public double circumference(){
    				return 2 * 3.14159 * z; 
    			}
    			public double area(){
    			 return 3.14159 * z*z;
    			}
    			public double radius(){
    				 return 2*z;
    				}
    			
    			
    			public static void main(String[ ] args) {
    				
    				
    				
    				int num[] =new int [args.length];
    				for (int i=0;i<args.length ;i++ ){  
    					
    				int k = Integer.parseInt(args[0]);
    				 num[i]=k; }
    
    				    
    				   
    				
    				Circleinput c1 = new Circleinput(x,y,z);
    				
    				
    				System.out.println("Flaeche c1: " + c1.area());
    				
    				System.out.println("Umfang c1: " + c1.circumference());
    				
    				System.out.println("Radius c1: " + c1.radius());
    				
    			} // main
    		} // class
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-16-2011 at 11:23 PM. Reason: Moderator Edit: Code tags added

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Most of your trouble is right here:
    Java Code:
            private double num[0],num[1],num[2];//Instance variables
    I have some vague idea what you're trying to do here, but you're not doing it, and it's not a good idea to do it anyway. If you want three separate instance variables, then declare three separate instance variables. If you want an array (hint: you don't) then declare an array.

    -Gary-

  3. #3
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thank you for your very quick help !!!

    1) But how can i input three String values without an array ???

    Where should I store the ???

    2) In this line you are asking me I try to put the values of the string (transformed to integers) to the parameters of the circle.

    I know that my level is very low and my teacher doesn't helps us a lot but how can I do it in another way ???

    Thanks in advance

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You want to get three double values from your main() method's args parameter. You're not interested in any arguments beyond the first three, so you don't really need args.length. You can simply do this:
    Java Code:
        public static void main(String[] args) {
            double x = Double.parseDouble(args[0]);
            // similar code for y and radius. If the argument is missing
            // or can't parse into a double, you will get an Exception.
        }
    You have other problems in your code as well, but deal with this first and give it another try.

    -Gary-

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  6. #6
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    I am sorry for the code. Now I know how to do it !!!

    But I can not understand if I make this changes like gcalvin says how it would be possible to enter three different values ???

    I need a for loop to give me this 3 values and store them in different variables !!!

    Am I right ???

  7. #7
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Ok i made it !!!!

    Thank you !!! I don't know if my code is very good but at leas it gives a result.


    Java Code:
    public class Circleinput {
    
    
    		
    			private double x,y,z;//Instance variables  
    			// The constructor method
    			public Circleinput(double x,double y ,double z) {
    				this.x = x;
    				this.y = y;
    				this.z = z;
    			}
    			public double circumference(){
    				return 2 * 3.14159 * z; 
    			}
    			public double area(){
    			 return 3.14159 * z*z;
    			}
    			public double radius(){
    				 return 2*z;
    				}
    			
    			
    			public static void main(String[ ] args) {
    				
    				
    				
    				 
    				        int x = Integer.parseInt(args[0]);
    				          int y = Integer.parseInt(args[1]);
    				             int z = Integer.parseInt(args[2]);
    
    
    				    
    				   
    				
    				Circleinput c1 = new Circleinput(x,y,z);
    				
    				
    				System.out.println("Flaeche c1: " + c1.area());
    				
    				System.out.println("Umfang c1: " + c1.circumference());
    				
    				System.out.println("Radius c1: " + c1.radius());
    				
    			} // main
    		} // class

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Itdepends upon how you want to store the values.

    Three separate variables: then simply have three statements to get user input and assign to each variable in turn.
    Java Code:
    var one = ...
    var two = ...
    var three = ...
    An array: then use a loop and assign user input to next slot in array.
    Java Code:
    loop {
        array[...] = ...
    }

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by iratus View Post
    I am sorry for the code. Now I know how to do it !!!

    But I can not understand if I make this changes like gcalvin says how it would be possible to enter three different values ???

    I need a for loop to give me this 3 values and store them in different variables !!!

    Am I right ???
    Why do you need a loop? What's wrong with this?
    Java Code:
        public static void main(String[] args) {
            double x = Double.parseDouble(args[0]);
            double y = Double.parseDouble(args[1]);
            double radius = Double.parseDouble(args[2]);
            // do the other stuff you want to do
        }
    Now generally, it's not a great idea to assume that main() will receive a specific number of arguments, but it seems to me that this case is a clear exception -- the faulty design has already been dictated to you, so you might as well accept it as it is. Your specification (unless I'm missing something) is that your program will get three doubles passed to it: x, y and radius. If there are fewer than three, or if they are not doubles, you'll get an Exception and the program will halt, which seems perfectly appropriate. If you get more than three arguments, you don't care about anything except the first three anyway.

    -Gary-

  10. #10
    aspire1 is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    Well it's open to intepretation the assignment, although I read it the same as gcalvin, pass the three values as command line arguments, run your program like:

    java Circleinput 25.1 12.4 50.5

    these three values will be in the array args[0], args[1], args[2]

    I don't read it that you have to get input from a user.

    It could also mean that you simply have to hardcode the values into your program:

    Circleinput c1 = new Circleinput(9.4,6.1,4.3);

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by iratus View Post
    Ok i made it !!!!

    Thank you !!! I don't know if my code is very good but at leas it gives a result.


    Java Code:
    public class Circleinput {
    
    
    		
    			private double x,y,z;//Instance variables  
    			// The constructor method
    			public Circleinput(double x,double y ,double z) {
    				this.x = x;
    				this.y = y;
    				this.z = z;
    			}
    			public double circumference(){
    				return 2 * 3.14159 * z; 
    			}
    			public double area(){
    			 return 3.14159 * z*z;
    			}
    			public double radius(){
    				 return 2*z;
    				}
    			
    			
    			public static void main(String[ ] args) {
    				
    				
    				
    				 
    				        int x = Integer.parseInt(args[0]);
    				          int y = Integer.parseInt(args[1]);
    				             int z = Integer.parseInt(args[2]);
    
    
    				    
    				   
    				
    				Circleinput c1 = new Circleinput(x,y,z);
    				
    				
    				System.out.println("Flaeche c1: " + c1.area());
    				
    				System.out.println("Umfang c1: " + c1.circumference());
    				
    				System.out.println("Radius c1: " + c1.radius());
    				
    			} // main
    		} // class
    It's certainly better than it was. Clean up the indentation -- neatness counts. And double-check this method:
    Java Code:
    	public double radius(){
    		return [COLOR="Red"]2*z[/COLOR];
    	}
    Really?

    -Gary-

Similar Threads

  1. Need help with java question
    By ccie007 in forum New To Java
    Replies: 23
    Last Post: 05-18-2010, 06:32 PM
  2. Java Question :D
    By thisisIT in forum New To Java
    Replies: 6
    Last Post: 03-12-2010, 04:04 PM
  3. Strange JSTL recordset result problem (Question)
    By jeremy.wilson in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 01-07-2010, 06:04 PM
  4. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  5. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •