• Post Reply Bookmark Topic Watch Topic
  • New Topic

Blue Pelican java lesson 15 project, What's that diameter?  RSS feed

 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blue Pelican java lesson 15 project, What's that diameter?

Hello I was wondering if someone could help me out with this program.

Create a new method for the circle class called diameter. Add this method to the circle class described on page 15-1. It should return a double that is the diameter of the circle. No parameters are passed to this method.

In a tester class, test the performance of your new diameter method as follows:
(Your project should have two classes, Tester and Circle.)

here is what i have so far:

 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a couple things to get you going in the right direction.

The project descriptions states to create a new METHOD within the Circle class called diameter. Right now on line 33 you are declaring diameter as a new class, not a method. Try refactoring line 33 so diameter is a method instead of a new class.

Second, you are using the variable "radius" in a few places in the Circle class. Consider declaring the radius variable as a private instance variable at the top of the Circle class definition.

This will at least get you going in the right direction. Once you have these changes complete, post the updates and we can help you with your progress.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says to add a method called diameter. I think it should really be called getDiameter, but you will have to do what the exercise tells you.
What you have done is create a class called Diameter, which is not what you have been told to do. Change the name of that class to Tester (which is again not a very good name).

Start by indenting your code properly; we have some suggestions here. Where it says public class, that should be as far to the left as it will go. Get rid of some of those empty lines which don't help legibiility. One empty line between methods, and no empty lines inside methods as short as yours. That way you will be able to see which parts of the class are which. Give the radius field private access.
Then add the diameter method, which presumably returns the diameter of your circle. It should be simple to implement. You managed an area method (which should be called getArea) so you should find a diameter method simple.
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Tester class is looking okay. The first System.out.println(); (line 46) is redundant and not needed here.

The Circle class could use some work. Have a look at the Java Tutorials on proper declaration of a class, method and instance variable.

You actually had the constructor correct in your previous version of the code (lines 3 through 9 of the old code). All you needed to do with the Circle class was to declare a new instance variable called "radius" of type double. Then declare the diameter() method somewhere below line 9.

Here is an example of some class, method, constructor, and instance variable declarations:
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am i getting close?

 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop guessing. If you guess a million times, as long as all the guesses are different, you will probably get the correct answer. You then have to look at your million guesses and work out which is correct
If you think, you can make one attempt and get a correct answer.

Were you getting near? No.

Look at the area method (even though it would be better called getArea). Look at what that returns. If you use a method like that to get the area, you can use a method like that to get the diameter. Only it will have a different name and a different formula inside. You are getting a lot closer in your latest post; the only problem is that r doesn't exist there. The radius variable does, however.

Now take the other hint and change the radius to private access. And you have another problem. For some reason you have moved the Circle class inside the Tester class. It is now an inner class, and I am sure you don't want it to be an inner class.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also indent your code correctly. Every { should be the same distance to the right as the line preceding it. The line following it should be one indent to the right. Every } should be one indent to the left from the start of the line preceding it. Then pairs of {} will align vertically with each other and you can see the structure of the code just by looking at it.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code copied, indented correctly, and with the excess blank lines removed, looks like this:-No other corrections made. It is awkward to indent your code because you have tab characters in it. Look at these suggestions. Look at the suggestions about editors here (be sure to read the links). You can set up the editor to do indentation automatically; you push the tab key and the editor indents by 4 spaces just like that.

You can see the structure of the code mush better because it is now indented properly and you can see that Circle has become an inner class.
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the above code is not running, i am getting these errors.

Tester.java:32: error: cannot find symbol
return radius * 2;
^
symbol: variable radius
location: class Tester.Circle
Tester.java:38: error: non-static variable this cannot be referenced from a static context
Circle cirl = new Circle (35.5);
^

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:the above code is not running, i am getting these errors.

Tester.java:32: error: cannot find symbol
return radius * 2;
^
symbol: variable radius


This error message does not match the latest code that you posted.

chavez james wrote:
location: class Tester.Circle
Tester.java:38: error: non-static variable this cannot be referenced from a static context
Circle cirl = new Circle (35.5);
^


Here is the explanation... however, I am not sure if you are ready for it, as I am going to guess that you didn't learn inner classes yet.

You created an inner class (not sure if accidental or not). And inner class instances require an instance of the outer class to exist. The main method is in the outer class class, but it is a static method -- and static content does not have an outer class instance (via the this reference) ... hence, the error message.

Henry
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure the creation of the inner class was accidental. Move the Circle class outside the Tester class, maintain correct indentation, and try again. Make sure radius is private and call the diameter method in the Tester#main method.
At least return radius * 2; looks correct for the contents of a getDiameter() method
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay it seems my code is good in the public class Circle besides the main. how can i take the 35.5 in the parameters and pass it using the object in my main where the method public double diameter takes the 35.5 multiplies it by 2 to output 71.0 in the main.

i am supposed to keep public class radius because it allows radius to be used in other methods.

 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote: . . . i am supposed to keep public class radius because it allows radius to be used in other methods.
. . .
Who on earth told you that sort of rubbish?

You make it private and provide a getRadius method. Since radius is a primitive, there can be no problems from that method. If you have radius public you can let somebody do this sort of thing:I still think you should make Circle into a top‑level class, but the circumference method now looks correct
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am working out of blue pelican book and the instructions are to use a public variable so it is available to all the methods in one single class.

Okay i got my code to run in two ways, 1. take the public class Circle and place it in its own file. 2. take out the word public from the Circle class. here i what i mean:

Tester.java

public class Tester
{

public static void main( String args[])
{
Circle cirl = new Circle (35.5);
System.out.println(cirl.diameter());
}

}

Circle.java

public class Circle
{
public double radius;

public Circle(double r)
{
radius = r;
}

public double diameter()
{
return radius * 2;
}

public double area()
{
double a = Math.PI * radius * radius;
return a;
}

public double circumference()
{
double c = 2 * Math.PI * radius;
return c;
}

}

or

public class Tester
{

public static void main( String args[])
{
Circle cirl = new Circle (35.5);
System.out.println(cirl.diameter());
}

}

class Circle
{
public double radius;

public Circle(double r)
{
radius = r;
}

public double diameter()
{
return radius * 2;
}

public double area()
{
double a = Math.PI * radius * radius;
return a;
}

public double circumference()
{
double c = 2 * Math.PI * radius;
return c;
}

}
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote: . . . book and the instructions are to use a public variable so it is available to all the methods in one single class. . . .
I hope that advice isn't typical of the book. Otherwise you have wasted however much you paid for it.

Yes, if you make a class non‑public you can put it in the same file as a public class. You can't however put two top‑level public classes in the same file. It seems to be working now an you no longer have the inner class.
 
Dee Hicks
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: And inner class instances require an instance of the outer class to exist. The main method is in the outer class class, but it is a static method -- and static content does not have an outer class instance (via the this reference) ... hence, the error message.


I think James and I might be in the same class. I have the same problem from the same stupid book (I use the one located at http://people.ucls.uchicago.edu/~bfranke/apcs_0809/downloads/BPJ_TextBook_3_0_5.pdf
) , but slightly different problems that I'm running into. Before I get to that:
Henry,
Can you explain the above quote to me, particularly "inner class instances require an instance of outer class to exist...and static content does not have an outer class instance"? I'm not sure I completely understand that.

In my class, we have a different book "Introduction to Programming in Java" in addition to the Blue Pelican above that doesn't seem to have much better explanations. It uses programming examples but sometimes doesn't even define what the syntax is or what each piece of the example program is doing, so I'm left to figure it out on my own or try to find better explanations on the Internet and I end up drowning in information.

Also in my class we are jumping around quite a bit, so it makes me feel like I'm missing a lot of the basic basic foundation.

I have programming experience in Visual Basic 16 years ago and an even older language that I'd rather not say <cough>Pascal</cough> for fear that it might reveal my age, and HTML if you call that programming, but I cannot find any straight forward Java instruction beyond oracle and https://buckysroom.org/videos.php?cat=31 which are great, very easy to understand but don't go much beyond the simplified examples. Do you have any recommendation of a beginning Java instruction book that might be able to explain some of this in simple English?
Dee
 
Dee Hicks
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Blue Pelican Book the exact instructions (on page 15-4) are:
Quote
Project… What’s That Diameter?
Create a new method for the Circle class called diameter. Add this method to the Circle class
described on page 15-1. It should return a double that is the diameter of the circle. No parameters
are passed to this method.
In a Tester class, test the performance of your new diameter method as follows:
(Your project should have two classes, Tester and Circle.)

end Quote

What I can't figure out is how do I get this thing to work without passing something to the method?
I can get mine to work if I declare--don't know if that's the correct terminology--something being passed to the method, but we're not supposed to do that. So I'm at an impasse.
I am trying to create mine in two different classes. In my class we are also not supposed to use any program that compiles for us, so we have to compile each program after we've completed it, then figure out what the error messages are referring to. I know the mistakes I tend to make so that makes my stupid little mistakes easier to find, but when I'm using the syntax wrong, I have no clue how to fix it. Then I search the Internet, wasting valuable time and end up drowning in too much information that sometimes seems conflicting and usually is not defined so I end up more confused.
This is the code we're given from the book to work with:

and (as shown above)

I can't believe that I've been working on THIS DAMN THING FOR Almost A WEEK AND I just got it to work using the books stupid code after one try. The only thing in my code that was not in the book's code is the diameter method. I just want to tear hear hair out!! Can anyone explain to me the line "Circle.cir1=new Circle(35.5);"?
Please.
I just resorted back to the book's code, because I have changed my code so much after one week that I can't even follow it myself or know what changes I made or what the original error was! I do know that I stripped both down to just the bare basics. I stripped the Circle down to just the diameter method and when I set "r = 11;" instead of "radius = r" and changed the diameter line variable to match and passed "r" to the method (which was against the assignment) I got it to work. But I couldn't figure out how to get the method to work without passing something to it. I guess now I know (kind of).
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You do realise that the inner class was probably a copying mistake on CJ's part? By putting the } in the wrong place he converted the Circle class into an inner class. Inner classes are a rather advanced feature which you don't need to know about at this stage. There is something about them in the Java Tutorials, where you find they are a particular kind of “nested classes”. You find that the inner class (not being marked static) is an instance member of the outer class. Just like other instance members (e.g. instance fields) the inner class has no existence until you create an object of the outer class.

HTML doesn't count as programming, at least not to TIOBE, but both Pascal and VB do. Pascal is a non‑object language and I have found a lot of people who program successfully in non‑object languages and struggle severely with Java because they can understand the syntax but can't understand objects. Changing a circle class by adding a getDiameter method should be a very simple action in object languages. All you have to do is return twice the radius. The pre‑existing methods to get the area and the circumference should act as hints. There is only one error in the code you have there, which appears to be in the book, viz that the radius field has public access. As good object design, all fields except those used as global constants (you have used a global constant in that code: here it is) have private access. Change the radius to have private access and allow other code to find out what size it is by adding a getRadius method like thisIn all cases you would get the radius by saying myCircle.getRadius(). That will work nicely for any type of primitive e.g. double, or immutable reference types e.g. String. But there are potential hazards if you return a mutable reference type, e.g. an array. Other code can alter the state of that object and therefore alter the state of your object indirectly. That is also the reason for not having radius public: there is no way you can ever take a precaution against code trying to change the radius of your circle to an obviously wrong value e.g. -999.99.

I shall shut up now for fear of overloading you with information.
 
Wayne Brehaut
Greenhorn
Posts: 4
Google Web Toolkit Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It says to add a method called diameter. I think it should really be called getDiameter, but you will have to do what the exercise tells you.


The prefix "get..." is better reserved just for direct attribute access (i.e., returning the value of a field). For calculations it's better to use "calculate...", as in calculateDiameter(). Then use getDiameter() to return the value of diameter (assuming its correctly declared as a field and not a method).

Then add the diameter method, which presumably returns the diameter of your circle. It should be simple to implement. You managed an area method (which should be called getArea)...


Or, better, calculateArea.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Something went wrong with the quote tags, but I have corrected it. I also changed all the text to black, because some people find the bright colours difficult to see.
Take your point about the method names. In this case the book instructed the method be called diameter(), however.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a look at the relevant section of the Blue Pelican book. The Tester class is exactly as has been given. The Circle class is exactly as was originally given, including the public radius field at the bottom of the class.

A number of things that you normally wouldn't do in a Java program are there but for instructional purposes, I guess this is fine. I don't happen to agree with them but I guess the author's approach is to introduce one concept at a time. You'd think that a proper getter method would have been introduced by the time you get to Lesson 15 but whatever.

Guys, the solution is a whopping 3 lines of code in addition to what has already been given by the book. How do you calculate the diameter of a circle, given the radius? How do write a method that returns a value? What is the name of the method that the book wants you to write to return the diameter of the circle? Answer these three questions and you'll have completed this "project".
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I said three lines based on the way I would do it. Based on the way the author does it, it would be five lines of code. You already have a template with the circumference method. Just do the same exact thing for a diameter method, except change the formula appropriately.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dee Hicks wrote:Can anyone explain to me the line "Circle.cir1=new Circle(35.5);"?

What part of the "Instantiating an object" section of that lesson is not clear?


Argh. Just noticed that this was an old thread that got re-awakened -- I hate it when that happens.

Wayne, I disagree that the "get" prefix is reserved for direct attribute access. Even though most getter methods do in fact provide direct attribute access, the point of a getter (or "accessor", if you prefer the more general term) is to hide the details of how the value was obtained. For all the client cares, the value could be calculated or it could come from another system via a web service call or it could be entered by some guy waiting at a terminal on the other side of the earth. You shouldn't assume that getters are doing direct attribute access and you shouldn't care.

As for using the "calculate" prefix instead of "get", I also disagree along the same lines as before. Good method names reveal intent and hide implementation details. When you have this kind of abstraction, the name will still be good even when the implementation details change. Adding "calculate" to the name makes it a kind of "leaky abstraction" because it suggests the way that a value is obtained. If the implementation changes and no calculation is in fact done, the name becomes misleading. For me, either diameter() or getDiameter() is fine but I would lean towards getDiameter() just to stick with JavaBean conventions.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Wayne!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!