11 Replies - 252 Views - Last Post: 23 March 2013 - 03:43 AM Rate Topic: -----

#1 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Type class is already declared?

Posted 22 March 2013 - 09:03 PM

Hello, I've been reading this site for a while now while learning C++ off and on, but now since my school has dropped C++ from the curriculum I'm starting Java and I ran into a problem so I decided to sign up. I have a homework problem where I need to prompt the user for a radius and a height of a cylinder, save the information in an array of objects, then print the results from the array. I've found a few examples of this problem in these forums' archives, but none are quite the same. Anyway, I think I got it, but I'm running into an error where it's telling me my Cylinder class is already defined. Please take a look.

import javax.swing.JOptionPane;

public class CylinderTest{

	static double aRadius;
	static double aHeight;
	
	public static void main(String[] args) {

		String outputString = "";

		Cylinder[] cylAr = new Cylinder[3];

		for (int i = 0; i < cylAr.length; i++) {
			
			while (true) {
				try {
					aRadius = Double.parseDouble(JOptionPane  //this is where I lose it.
							.showInputDialog("Please, enter cylinder "
									+ (i + 1) + "'s radius: "));
				} catch (NumberFormatException z) {
					JOptionPane.showMessageDialog(null,
							"Sorry, invalid input.", "Invalid",
							JOptionPane.ERROR_MESSAGE);
					continue;
				}
				if(aRadius <= 0){
					JOptionPane.showMessageDialog(null, "Radius cannot be zero or less.", "Invalid", JOptionPane.ERROR_MESSAGE);
					continue;
				}
				cylAr[i].setRadius(aRadius);
				break;
			}	
			
			while (true) { 
				try { 
					aHeight = Double.parseDouble(JOptionPane
							.showInputDialog("Please, enter cylinder "
									+ (i + 1) + "'s height: "));
				} catch (NumberFormatException z) {
					JOptionPane.showMessageDialog(null,
							"Sorry, invalid input.", "Invalid",
							JOptionPane.ERROR_MESSAGE);
					continue;
				}
				if(aHeight <= 0){
					JOptionPane.showMessageDialog(null, "Height cannot be zero or less.", "Invalid", JOptionPane.ERROR_MESSAGE);
					continue;
				}
				cylAr[i].setHeight(aHeight);
				break;
			}
			
			cylAr[i] = new Cylinder(cylAr[i].getRadius(), cylAr[i].getHeight());
		}
		
		for(int j = 0; j < cylAr.length; j++){
			outputString += "Cylinder " + (j + 1) + ": \n" + "Radius: "
					+ cylAr[j].getRadius() + " \n" + "Height: " + cylAr[j].getHeight() + " \n"
					+ "Volume: " + cylAr[j].getVolume() + "\n-------------------\n";

		}

		JOptionPane.showMessageDialog(null, outputString, "Complete!",
				JOptionPane.INFORMATION_MESSAGE);

	
}
	
}

class Cylinder {

	private double radius;
	private double height;

	public Cylinder(double radius, double height) {
		this.radius = radius;
		this.height = height;

	}

	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double aRadius) {
		aRadius = radius;
	}

	public double getHeight() {
		return height;
	}
	
	public void setHeight(double aHeight){
		aHeight = height;
	}

	public double getVolume() {
		return (radius * radius * height * Math.PI);
	}

}


So what's up? Will my program work like I think it will? I need to add DecimalFormat yet but I want it to work first. Thanks either way.

Is This A Good Question/Topic? 0
  • +

Replies To: Type class is already declared?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Type class is already declared?

Posted 23 March 2013 - 01:53 AM

Your code looks fine at the "will it compile" level, so somewhere in your development environment, the class Cylinder already exists. A couple quick fixes would be to put what you posted into its own package or rename the Cylinder references in this program to something else, e.g. MyCylinder.

Depending on the bells and whistles present in your editor, you may be able to hover on the word "Cylinder" in your code and determine where the original Cylinder is. Since there's not a Cylinder class in core Java or in the other common libraries that I tried, it's probably an older version of yours stored in the same directory or on the class path.
Was This Post Helpful? 1
  • +
  • -

#3 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Type class is already declared?

Posted 23 March 2013 - 02:10 AM

View PostGregBrannon, on 23 March 2013 - 01:53 AM, said:

Your code looks fine at the "will it compile" level, so somewhere in your development environment, the class Cylinder already exists. A couple quick fixes would be to put what you posted into its own package or rename the Cylinder references in this program to something else, e.g. MyCylinder.

Depending on the bells and whistles present in your editor, you may be able to hover on the word "Cylinder" in your code and determine where the original Cylinder is. Since there's not a Cylinder class in core Java or in the other common libraries that I tried, it's probably an older version of yours stored in the same directory or on the class path.


Yeah, I feel really dumb now for not thinking about that...

I had originally had Cylinder in a different file, and then the Prof updated the requirements wanting it all in one source. That was simple...

However, now that it 'works', it's giving me a NullPointerException and I believe pointing to the

cylAr[i].setRadius(aRadius);


At least that's what it highlights. Which leaves me pretty lost.

Do I need to do it like this? The homework doesn't mention anything about using setter methods anyway, I just couldn't think of another way to do it...
Was This Post Helpful? 0
  • +
  • -

#4 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Type class is already declared?

Posted 23 March 2013 - 02:24 AM

many many thanks by the way. I don't supposed there's an edit post button I can't see?
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Type class is already declared?

Posted 23 March 2013 - 02:28 AM

Don't feel dumb. You've likely been handicapped by your knowledge of C++. (j/k).

The source of the error is as you indicated, but the reason you're getting the NPE is because this line:

Cylinder[] cylAr = new Cylinder[3];

creates the necessary memory space for an array that holds 3 Cylinder objects, but it doesn't actually create the Cylinder objects and stick them in the array.

To fix, my first thought was to create a new Cylinder object in the array each pass through the first for loop, as in:
for (int i = 0; i < cylAr.length; i++)
{
    cylAr[i] = new Cylinder();

    // etc . . .

but that doesn't work, because you don't have a default, zero-argument constructor. Then your final question made more sense.

Using setters is a fine way to define the particulars of an object once the object exists, but in your case 1) the objects don't exist, and 2) you have a Cylinder constructor that will do the same thing.

Remove the set() statements and use the constructor to define the Cylinder objects in the array, after the user has provided the radius and height for each one:
	public Cylinder(double radius, double height) {
		this.radius = radius;
		this.height = height;

so the statement at the end of the first for loop should be:
cylAr[i] = new Cylinder(radius, height);

Hope that's clear enough, but ask if not.

This post has been edited by GregBrannon: 23 March 2013 - 02:32 AM
Reason for edit:: Fixed typos.

Was This Post Helpful? 1
  • +
  • -

#6 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Type class is already declared?

Posted 23 March 2013 - 02:43 AM

Ok, that does make it somewhat clearer at least. My problem before deciding to use the setters was that I didn't know how to collect the input using showInputDialog past using a basic variable. I thought it would be this.radius =, or Cylinder.radius =, but both of those do not work. What is the syntax I'm looking for, and am I missing something constructor-wise in the CylinderTest class?

Thank you, again.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Type class is already declared?

Posted 23 March 2013 - 02:45 AM

View PostHGPolumbus, on 23 March 2013 - 05:24 AM, said:

many many thanks by the way. I don't supposed there's an edit post button I can't see?

The Edit button appears after you've made a certain number of posts, somewhere in the mid teens, I think. A newcomer's tendency is to feel stupid once the "truth" has been revealed and to erase all evidence of their stupidity, but that eliminates the usefulness of the post to the 100 (or so) others who have or will have the same question but won't ask, and it makes the while thread pointless. Plus, if you erased or significantly altered the question, my answer would make no sense, so it would be my time wasted, and that would aggravate all who volunteer their time here.

Apparently, the DIC staff psychologist determined that after some number of posts we realize that we're all allowed to make mistakes and that the community gently corrects them, most of the time. If you experience otherwise, you're welcome to report the offender(s).
Was This Post Helpful? 1
  • +
  • -

#8 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Type class is already declared?

Posted 23 March 2013 - 02:47 AM

View PostGregBrannon, on 23 March 2013 - 02:45 AM, said:

View PostHGPolumbus, on 23 March 2013 - 05:24 AM, said:

many many thanks by the way. I don't supposed there's an edit post button I can't see?

The Edit button appears after you've made a certain number of posts, somewhere in the mid teens, I think. A newcomer's tendency is to feel stupid once the "truth" has been revealed and to erase all evidence of their stupidity, but that eliminates the usefulness of the post to the 100 (or so) others who have or will have the same question but won't ask, and it makes the while thread pointless. Plus, if you erased or significantly altered the question, my answer would make no sense, so it would be my time wasted, and that would aggravate all who volunteer their time here.

Apparently, the DIC staff psychologist determined that after some number of posts we realize that we're all allowed to make mistakes and that the community gently corrects them, most of the time. If you experience otherwise, you're welcome to report the offender(s).



Haha! Well I can understand that, but my stupidity is here for the world to see! I only wanted to edit to add my 'thank you' without making another post.
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Type class is already declared?

Posted 23 March 2013 - 02:54 AM

View PostHGPolumbus, on 23 March 2013 - 05:43 AM, said:

Ok, that does make it somewhat clearer at least. My problem before deciding to use the setters was that I didn't know how to collect the input using showInputDialog past using a basic variable. I thought it would be this.radius =, or Cylinder.radius =, but both of those do not work. What is the syntax I'm looking for, and am I missing something constructor-wise in the CylinderTest class?

The constructor you have is adequate, but a default no-argument constructor never hurts. It can be used to set default values or for some other useful purpose.
I'm not really sure what you mean by your last question, so I just fixed your first for loop to do what I described, posted below. Note that I commented out two lines of code and changed the final line of the for loop as I suggested above. Really minor changes, but let me know if you have any questions:
for (int i = 0; i < cylAr.length; i++)
{
			while (true) {
				try {
					aRadius = Double.parseDouble(JOptionPane  //this is where I lose it.
					.showInputDialog("Please, enter cylinder "
							+ (i + 1) + "'s radius: "));
		} catch (NumberFormatException z) {
			JOptionPane.showMessageDialog(null,
					"Sorry, invalid input.", "Invalid",
					JOptionPane.ERROR_MESSAGE);
			continue;
		}
		if(aRadius <= 0){
			JOptionPane.showMessageDialog(null, "Radius cannot be zero or less.", "Invalid", JOptionPane.ERROR_MESSAGE);
			continue;
		}
		// cylAr[i].setRadius(aRadius);
		break;
	}	

	while (true) { 
		try { 
			aHeight = Double.parseDouble(JOptionPane
					.showInputDialog("Please, enter cylinder "
							+ (i + 1) + "'s height: "));
		} catch (NumberFormatException z) {
			JOptionPane.showMessageDialog(null,
					"Sorry, invalid input.", "Invalid",
					JOptionPane.ERROR_MESSAGE);
			continue;
		}
		if(aHeight <= 0){
			JOptionPane.showMessageDialog(null, "Height cannot be zero or less.", "Invalid", JOptionPane.ERROR_MESSAGE);
			continue;
		}
		// cylAr[i].setHeight(aHeight);
		break;
	}

	cylAr[i] = new Cylinder(aRadius, aHeight);
}

Was This Post Helpful? 1
  • +
  • -

#10 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Type class is already declared?

Posted 23 March 2013 - 03:15 AM

Ok... ok... well it works, and for that I thank you. I would like to ask a follow up question about how it works if I could steal a little bit more of your time.

About the "syntax I'm looking for...", I meant that without the setter methods, aRadius and aHeight don't really mean anything... so how are they using the radius and height from the other class? What could I put in place of those variables to clean it up some, or does there have to be a local variable?

To continue along the same lines, in my output section, how does getRadius() know the correct value to return? Does aRadius just become radius through the constructor?
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Type class is already declared?

Posted 23 March 2013 - 03:33 AM

Ahh. Thanks for explaining the question.

The values aRadius and aHeight are collected from the user in CylinderTest and passed to the Cylinder() constructor with the 'new' statement:

cylAr[i] = new Cylinder(aRadius, aHeight);

The Cylinder() constructor then uses the parameters, aRadius and aHeight to define the radius and height instance variables for each instance of the Cylinder. Looking at the constructor:
public Cylinder(double radius, double height)
{
	this.radius = radius;
	this.height = height;
}

Note that the parameters radius and height will be the values passed from the 'new' statement, the current values of aRadius and aHeight. The constructor then takes those values of aRadius and aHeight and assigns them to the Cylinder instance variables radius and height with the statements:

this.radius = radius;
this.height = height;

Later, the getters return those values for the specific Cylinder reference using the statement:

cylAr[j].getRadius();

cylAr[0] - cylAr[2] are specific references to different Cylinder objects, each pointing to different areas of memory that have been reserved to store each object and all of its variables. The program, using the magic of OOP, knows exactly what to return based on what was asked for.

I'm trying to explain or outline some pretty advanced concepts in very few and probably inadequate words, so I wouldn't be surprised if you still have big question marks over your head. The best way for it to sink in is to keep coding, thinking about what's happening, and asking questions when you don't get it. The answers won't always completely make it clear to you, but over time the clouds will lift.
Was This Post Helpful? 1
  • +
  • -

#12 HGPolumbus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 22-March 13

Re: Type class is already declared?

Posted 23 March 2013 - 03:43 AM

View PostGregBrannon, on 23 March 2013 - 03:33 AM, said:

I'm trying to explain or outline some pretty advanced concepts in very few and probably inadequate words, so I wouldn't be surprised if you still have big question marks over your head. The best way for it to sink in is to keep coding, thinking about what's happening, and asking questions when you don't get it. The answers won't always completely make it clear to you, but over time the clouds will lift.


No, I think I definitely get it, or at least it's starting to sink in. I think that I knew the answers to my own questions, but I needed to hear it from someone who definitely knew what they were talking about.

Works great. You have been an intense amount of help, thank you thank you for all the speedy and educational replies. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1