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

Thread: Please help desperate beginner with Finding Volume project.

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

    Default Please help desperate beginner with Finding Volume project.

    I'm new to java but have a project due tomorrow that's getting a bit out of my league. Posting on forums is my last ditch effort to figure this out.

    This is an applet program has 4 entry fields for radius, length, width, and height. It then allows you to click on one of 4 options: box, cylinder, cone, or sphere. It will then tell you volume of what you selected.

    My code is without a doubt messier than it needs be for this is basically me adding and swapping to a preexisting program.

    I'm getting an error that I can't seem to figure out. It doesn't like these 2 user defined methods but has no issue with the radius and length ones.

    Error:

    C:\Users\pat\Desktop\FindVolume.java:127: error: getWidth() in FindVolume cannot override getWidth() in Component
    public double getWidth()
    ^
    return type double is not compatible with int
    C:\Users\pat\Desktop\FindVolume.java:136: error: getHeight() in FindVolume cannot override getHeight() in Component
    public double getHeight()
    ^
    return type double is not compatible with int
    2 errors

    Tool completed with exit code 1



    I'm also having a hard time getting it to allow me to leave a field blank. I need this because some volumes need radius and some dont and etc etc. But the instructions are to also not allow it to be <= 0. So, I just don't know how to code this.

    Here's my code, any help would be greatly appreciated.

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
     
    public class FindVolume extends Applet implements ItemListener
    {
    	//declare variables and construct a color
    	double rUnits, lUnits, wUnits, hUnits, answer;
    	int objCode;
    	Color custColor = new Color(0, 50, 160);
     
    	// create components for applet
    	Label radiusLabel = new Label("Enter radius of object:");
    		TextField radiusField = new TextField(15);
     
    	Label lengthLabel = new Label("Enter length of object:");
    		TextField lengthField = new TextField(15);
     
    	Label widthLabel = new Label("Enter width of object:");
    		TextField widthField = new TextField(15);
     
    	Label heightLabel = new Label("Enter height of object:");
    		TextField heightField = new TextField(15);
     
     
    	Label codeLabel = new Label("Select the object you want volume for:");
     
    	CheckboxGroup codeGroup = new CheckboxGroup();
    		Checkbox boxBox = new Checkbox("Box",false,codeGroup);
    		Checkbox cylinderBox = new Checkbox("Cylinder",false,codeGroup);
    		Checkbox coneBox = new Checkbox("Cone",false,codeGroup);
    		Checkbox sphereBox = new Checkbox("Sphere",false,codeGroup);
    		Checkbox hiddenBox = new Checkbox("",true,codeGroup);
     
    	Label outputLabel = new Label("Click an option to calculate volume.");
     
    	public void init()
    	{
    		setBackground(custColor);
    		setForeground(Color.white);
    		add(radiusLabel);
    		add(radiusField);
    		radiusField.requestFocus();
    		radiusField.setForeground(Color.black);
    		add(lengthLabel);
    		add(lengthField);
    		lengthField.requestFocus();
    		lengthField.setForeground(Color.black);
    		add(widthLabel);
    		add(widthField);
    		widthField.requestFocus();
    		widthField.setForeground(Color.black);
    		add(heightLabel);
    		add(heightField);
    		heightField.requestFocus();
    		heightField.setForeground(Color.black);
    		add(codeLabel);
    		add(boxBox);
    		boxBox.addItemListener(this);
    		add(cylinderBox);
    		cylinderBox.addItemListener(this);
    		add(coneBox);
    		coneBox.addItemListener(this);
    	    add(sphereBox);
    		sphereBox.addItemListener(this);
    		add(outputLabel);
     
    	}
     
    	// this method is triggered by the user clicking the option button
    	public void itemStateChanged(ItemEvent choice)
    	{
    		try
    		{
    			rUnits = getRadius();
    			lUnits = getLength();
    			wUnits = getWidth();
    			hUnits = getHeight();
    			objCode = getCode();
    			answer = getVol(rUnits,lUnits,wUnits,hUnits,objCode);
    			output(answer, rUnits, lUnits, wUnits, hUnits);
    		}
     
    		catch(NumberFormatException e)
    		{
    			outputLabel.setText("You must enter a number >= zero.");
    			hiddenBox.setState(true);
    			radiusField.setText("");
    			radiusField.requestFocus();
    			lengthField.setText("");
    			lengthField.requestFocus();
    			widthField.setText("");
    			widthField.requestFocus();
    			heightField.setText("");
    			heightField.requestFocus();
    		}
    	}
     
    	public double getRadius()
    	{
    		double radius = Double.parseDouble(radiusField.getText());
     
    		if (radius <= 0) throw new NumberFormatException();
     
    		return radius;
    	}
     
    	public double getLength()
    	{
    		double length = Double.parseDouble(lengthField.getText());
     
    		if (length <= 0) throw new NumberFormatException();
     
    		return length;
    	}
     
    	public double getWidth()
    	{
    		double width = Double.parseDouble(widthField.getText());
     
    		if (width <= 0) throw new NumberFormatException();
     
    		return width;
    	}
     
    	public double getHeight()
    	{
    		double height = Double.parseDouble(heightField.getText());
     
    		if (height <= 0) throw new NumberFormatException();
     
    		return height;
    	}
     
    	public int getCode()
    	{
    		int code = 0;
    		if (boxBox.getState()) code = 1;
    		else
    			if (cylinderBox.getState()) code = 2;
    			else
    				if (coneBox.getState()) code = 3;
    				else
    					if (sphereBox.getState()) code = 4;
    		return code;
    	}
     
     
    	public double getVol(double radius, double length, double width, double height, int code)
    	{
    		double volume = 0.0;
    		switch(code)
    		{
    			case 1:
    				volume = length * width * height;
    				break;
     
    			case 2:
    				volume = Math.PI * height * radius * radius;
    				break;
     
    			case 3:
    				volume = (Math.PI * radius * radius * height) / 3;
    				break;
     
    			case 4:
    				volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
    		}
    		return volume;
    	}
     
    	public void output(double volume, double radius, double length, double width, double height)
    	{
    		DecimalFormat twoDigits = new DecimalFormat("###.##");
    		outputLabel.setText("Volume of selected object " + " = " + twoDigits.format(volume));
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help desperate beginner with Finding Volume project.

    Quote Originally Posted by phuzed View Post
    C:\Users\pat\Desktop\FindVolume.java:127: error: getWidth() in FindVolume cannot override getWidth() in Component
    public double getWidth()
    ^
    return type double is not compatible with intC:\Users\pat\Desktop\FindVolume.java:136: error: getHeight() in FindVolume cannot override getHeight() in Component
    public double getHeight()
    ^
    return type double is not compatible with int
    You can not "automatically" convert from double to int because there is a possibility of data loss. (Everything after the decimal is dropped) If you want to use the value as an integer, cast it to an integer. There is also the complaint of overriding a method from Component. So don't override the method.

    Quote Originally Posted by phuzed View Post
    I'm also having a hard time getting it to allow me to leave a field blank. I need this because some volumes need radius and some dont and etc etc. But the instructions are to also not allow it to be <= 0. So, I just don't know how to code this.
    If the method signature requires a value for radius, then you must supply a value for radius when you call the method. If you need a method to do two different things, then you really need two different methods. Methods do exactly one thing.

Similar Threads

  1. Need help with parameter passing in my project? (Beginner)
    By xlFireman in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 14th, 2012, 01:10 AM
  2. Beginner Project
    By Saberunna in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2011, 08:49 PM
  3. Desperate Need of help
    By mszyndlar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2011, 03:32 AM
  4. Sphere (Volume/Surface Area)
    By Alex L in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 5th, 2011, 02:51 PM
  5. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 08:52 AM