Results 1 to 4 of 4
  1. #1
    aldorfski_17 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Major issues with code - Please help

    Hello All,

    I'm having some issues creating objects from one class and using these in another class. Basically, I created a class named Circle that will hold all of the constructor, variable, and method data for calculating the circumference and area of a circle. In the class named CircleApp, I need to call the objects and methods from Circle to run my program. I am having issues with the names of my parameters that I am setting, and I am having issues with the syntax needed to create the object in class CircleApp. Please read the two sets of code below. Any help is greatly appreciated.

    Circle Class:
    Java Code:
    import java.lang.Math;
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    
    
    	public class Circle
    	{
    		// declared and initialized a private static int type for ObjectCount
    		private static int objectCount = 0;
    		// declared a constant for PI
    		public static final double PI = 3.141592653589793;
    		
    		// Create Scanner object
    		Scanner sc = new Scanner(System.in);
    		double circleRadius = sc.nextDouble();
    		
    		// declared two variables to be used to calculate Area and Circumference
    		private double circleArea;
    		private double circleCircumference;
    		
    		// Constructor for Circle object
    		public Circle()
    	    {
    	        this.circleArea = 0;
    	        this.circleCircumference = 0;	        
    	    }
    		
    	// Constructor with parameters
    	public Circle(Scanner sc2, Scanner sc3)
    	{
    		this.circleCircumference = sc4;	
    		this.circleArea = sc5;
    	}
    		// Method to get Circumference 
    		public double getCircumference(double circumference)
    		{
    			double circleCircumference = 0;
    			circleCircumference = PI * 2 * circleRadius;	
    			
    			return circleCircumference;
    		}
    		
    		// Method to Format Circumference
    		public String getFormattedCircumference()
    		{
    			double circleCircumference = 0;
    			NumberFormat circleNumber = NumberFormat.getNumberInstance();
    			circleNumber.setMaximumFractionDigits(2);
    			String circleString = circleNumber.format(circleCircumference);
    			
    			return circleString;
    		}
    		
    		// Method to get Area
    		public double getArea(Double radius)
    		{
    			double circleArea = 0;
    			circleArea = PI * (circleRadius * circleRadius);
    			
    			return circleArea;
    		}
    		
    		// Method to format Area number
    		public String getFormattedArea()
    		{
    			double circleArea = 0;
    			NumberFormat circleNumber2 = NumberFormat.getNumberInstance();
    			circleNumber2.setMaximumFractionDigits(2);
    			String circleString2 = circleNumber2.format(circleArea);
    			
    			return circleString2;
    		}
    		// method needed but unsure of how it should be used
    		private String formatNumber(double x)
    		{
    			return null;
    		}
    		// method to return objectCount
    		public static int getObjectCount()
    		{
    			return objectCount;		
    		}
    }


    CircleApp Class:
    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    
    public class CircleApp {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
    		{
    			// Output header
    			System.out.println("Welcome to the Circle Tester");
    			System.out.println();
    			
    			// Create Scanner object
    			Scanner sc2 = new Scanner(System.in);
    			
    			// Set String choice to "y"
    			String choice = "y";
    			
    			// Include a while loop in order to check the string choice. If "y" or "Y," 
    			// then calculate Area and Circumference
    			while (choice.equalsIgnoreCase("y"))
    			{
    				
    				// Output prompt to input a radius
    				System.out.print("Enter Radius: ");
    				Circle circleCircumference = new Circle(sc2);
    				System.out.println("Circumference: " + circleCircumference);
    				System.out.println("Area: ");
    				
    							
    				// see if the user wants to continue
    	            choice = Validator.getString(sc2, "Continue? (y/n): ");
    	            System.out.println();
    	            
    	            // include an if statement to test if the string choice is "n" or "N"
    	            // if "n" or "N," then the program will end and count the total number of objects
    	            if (choice == "n")
    	            {
    	            	System.out.println("Goodbye");
    	            	System.out.println("Object Count:    " + Circle.getObjectCount() + "\n"); 
    	            }
    			}
    								
    			}
    
    	}
    As you can see, I do not know how to call the Circumference and Area from the Circle Class that I created. Any help is appreciated.

    -Aldorfski
    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-28-2010 at 05:34 PM. Reason: Moderator Edit: Code tags added

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

    Default

    Code tags have been added. Please see my signature below to learn how to do this yourself in your next post.

    Alright, with compile errors, go to the line that the compiler is complaining about, read the error carefully, and usually you'll find out what's wrong. For instance, I'm sure your compiler complained about these two lines:
    Java Code:
      public Circle(Scanner sc2, Scanner sc3) {
        this.circleCircumference = sc4;  // *** here
        this.circleArea = sc5;  // *** here
      }
    Do you see what's wrong here? Where do sc4 and sc5 come from? Why are you using Scanner objects as method parameters? What do your instructions tell you should be used? Also, if you have defined a circle's area can you independently define the circumference (and visa versa with circumference first then area), or is one dependent on the other?

    Keep working on the compiler errors first, and then come on back if you have a question about an error. Please post the error message itself and indicate which line is causing it.

    Luck.
    Last edited by Fubarable; 03-28-2010 at 05:40 PM.

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

    Default

    More recommendations on your code

    1) Make Circle a class that just represents a circle and its properties. Don't put any user interaction (such as Scanner code) in this class. This way you can use this class in situations that don't require user interaction, or even later can use it in Swing apps where Scanner isn't often used. Have all user interaction occur in the CircleApp class.

    2) I'd give Circle a double radius field and would get rid of the area and circumference fields. If any one of the three of these is defined then it forces the others to be defined as well. In other words, you can't give a Circle object a radius of 3 and an area of 2. Once you've defined the radius, the area must be the radius squared times Pi. You could make any one of the three values the field, but the radius seems more intuitive to me. I'd then make the getRadius() method a true getter where it returns the value in the radius field, and the getCircumference() and getArea() methods return calculated results. If you need a setCircumference(double c), you can have it, just in the method use the parameter to calculate what the radius should be and set the radius field.

    3) Again, your class has no business having a constructor that accepts two Scanners as parameters. It just makes no sense.

    4) There's no need to define PI in your class as it's already done for you with a constant from the Math class: Math.PI.

  4. #4
    aldorfski_17 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Thanks

    Hey,

    Thanks for your help. I guess the main issue is that I had to structure this according to how it was set up. I was able to make a few changes and it compiles now. I couldn't get the exact structure to work in that manner, but at least I can run the program. Thanks for the insight.

    -Aldorfski

Similar Threads

  1. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  2. Major number
    By lobodelbosque in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 05:55 AM
  3. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  4. Software Engineer...Computer Science Major
    By giganews35 in forum Introductions
    Replies: 2
    Last Post: 09-14-2008, 09:19 AM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 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
  •