Results 1 to 4 of 4
- 03-28-2010, 05:22 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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:
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.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"); } } } }
-Aldorfski
Moderator Edit: Code tags addedLast edited by Fubarable; 03-28-2010 at 05:34 PM. Reason: Moderator Edit: Code tags added
-
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:
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?Java Code:public Circle(Scanner sc2, Scanner sc3) { this.circleCircumference = sc4; // *** here this.circleArea = sc5; // *** here }
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.
-
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.
- 03-29-2010, 04:46 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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
-
jdk issues
By artemff in forum New To JavaReplies: 3Last Post: 01-02-2010, 03:18 AM -
Major number
By lobodelbosque in forum New To JavaReplies: 1Last Post: 11-27-2009, 05:55 AM -
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
Software Engineer...Computer Science Major
By giganews35 in forum IntroductionsReplies: 2Last Post: 09-14-2008, 09:19 AM -
Issues with Jva I.O
By Annatar01 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks