• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help on a switch and others

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im writing a program to simulate a car rental company. I have a rating key that takes in a set of letters using a switch statement and only in uppercase. My prof has said to use a static method to convert any lower cases to upper, before they reach the method and in the computeDailyFee() it should take the given key and mulitply it by the factor based on the key.

What he told us to do./**

To deal with this uppercase/lowercase problem: When you store the rating key – both in the
constructor and in the respective mutator – convert it to uppercase. This has the nice effect
that in computeDailyFee() you can be sure that it's uppercase and you don't have to worry
about this anymore*/


So my question is, I have done up to the point where I understand the material and now I'm kind of lost at sea.

Firstly its saying something is wrong in my switch method and after that, I'll need to see if the rest is correct.

Also am I using the static toUpperCase correctly?

I was also thinking I could just set my switch statemeent so that it read the lower case versions and then defaults on anything else, but he said to do it with the toUppercase thing, so ....I know the switch would be easier.


So as of now, I'd just like help on the switch statement and then after that I'll ask again if I can't figure out the rest.

Its getting easier as I do more, wrote this in about 30-40mins.

I'd already seen the set error I made, I fixed it, but forgot to fix on here.



**updated again
***again edited toUpperCase
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Staves wrote: Its getting easier as I do more, wrote this in about 30-40mins.

This won't compile.

I'm not sure what you mean when you ask if you're "using toUpperCase" correctly; number one, I don't see you using it anywhere, and number two, you're not defining a method where you believe you are:I mean, you should be getting a wide variety of compilation errors for this code.

How about write one little tiny bit at a time, see if it works, *then* add on more code? Why try to fix everything at once?!
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:


I'm not sure what you mean when you ask if you're "using toUpperCase" correctly; number one, I don't see you using it anywhere, and number two, you're not defining a method where you believe you are:I mean, you should be getting a wide variety of compilation errors for this code.

How about write one little tiny bit at a time, see if it works, *then* add on more code? Why try to fix everything at once?!



I'd thought using it as such would do it, what do I need to call on the toUpperCase? in my setRatingKey.... Im not trying to fix everything at once, I asked for what I needed help with.

I'd a;ready fixed that set method you pointed out. I'd just forgotten to correct it in my code pasted on here..
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Mr. Newton. You have over 20 compiler errors. Look at the following:
1. As to your mutators, you cannot assign a method name to a variable. You need the mutator to accept a parameter and then assign the parameter to the variable.
2. In your switch statement, the variable on which you switch must be defined. Also, in the case part, you must assign your numbers to a variable. Finally, the method that holds your switch statement expects you to return a double. Therefore, return a double at the end of the method.
3. You also have a typo for one of your variables in your mutators
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As to the upper case issue, take a look at java.lang.Character. There is a static function that does what you need.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:I agree with Mr. Newton. You have over 20 compiler errors. Look at the following:
1. As to your mutators, you cannot assign a method name to a variable. You need the mutator to accept a parameter and then assign the parameter to the variable.
2. In your switch statement, the variable on which you switch must be defined. Also, in the case part, you must assign your numbers to a variable. Finally, the method that holds your switch statement expects you to return a double. Therefore, return a double at the end of the method.
3. You also have a typo for one of your variables in your mutators



the only errors my compiler is giving me is the switch statement.

But thanks for the pointouts.


ahhhh, ok. Thank you, that helps for the switch statement. I wasnt sure what to do, I just learned it this week.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:As to the upper case issue, take a look at java.lang.Character. There is a static function that does what you need.



I have the static in their.



Im just not sure on how to use it.

I corrected most and it compiles with only 1 error now. Im not sure how to use the toUpperCase static.

I changed it again, Im not sure, I mean I've already looked at the API on it, but what is that showing me?

I think my latest fix is the rightt path, but unsure how to fix.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not "using" anything there--you're (sort of) defining a new method or something, but incorrectly.

If you're trying to *use* a static method from somewhere, pick where you're going to use it (not in the middle of a class definition) and use the normal syntax for calling a static method; NameOfClass.nameOfStaticMethod(methodParams).
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At this point you need to understand what the compiler is telling you:

This method requires a body instead of a semicolon

Take a look at the rest of your methods. Do any just end with a semicolon? Hint: you must actually process the input parameter and return a char that is in upper case. Double hint: think about using java.lang.Character.toUpperCase() to process your input. Or, as David suggests, don't just define a static method, have your code that would typically call your static method call Character.toUpperCase() instead. (Which is a static method and therefore conforms to what your professor told you to do.)
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:You're not "using" anything there--you're (sort of) defining a new method or something, but incorrectly.

If you're trying to *use* a static method from somewhere, pick where you're going to use it (not in the middle of a class definition) and use the normal syntax for calling a static method; NameOfClass.nameOfStaticMethod(methodParams).



Something like this , public static CarRental.toUpperCase(char ratingKey);

Im sorry if this is annoying, but Im just not really grasping the concept of how it works.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im sooo....lost...I still dont get it, so I need to attach the toUpperCase to something..... that I dont even understand how to use...

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in your original post you say:

To deal with this uppercase/lowercase problem: When you store the rating key – both in the
constructor and in the respective mutator – convert it to uppercase. This has the nice effect
that in computeDailyFee() you can be sure that it's uppercase and you don't have to worry
about this anymore*/

This means that whenever you set the rating key variable, you need to convert it to upper case. The rating key is set in two places; in the constructor and in the mutator. I see you edited your original post to update the code. You almost got the mutator right. You still need to get the constructor right. See my previous post on how to fix your static toUpperCase() method. In your mutator, try something like this:or
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much tom. Ok so that'll take care of the toUpperCase. Now is my double fee actually going to work, it compiles now, so can I start on my tester?

the showing me what and where to do it really helped. thank you both alot.



 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So two problems.

First------------------
/** what my prof told us.
To round a double to two digits, do the following:
1. Multiply it with 100.0 (makes it the same amount, but in cents) and then round this
via Math.round(...).
2. Store this into a long and then divide it back by 100.0 (turning it back into dollars)
into the final double.*/

got my tester working, but the
*** double fee = Math.round(((daily*ratingKey)*100.0))/100.0; ***is not working correctly any ideas?
It should work, I did it by hand and it comes out. When I do it like so, daily(92.76) * 100, then * by the factor say 1.6 = 14841.6/ by 100= 148.416 and then round would be a 2 digit number, but my java formula no matter how I write it wont come out like this
.
this and how do I make it be called whenever the .computeDailyFee() is called on. currently its returning just the switch statements number. My idea is to replace the return x in the switch with return fee;
-------------------------
Secondly


in my tester...

So, You just have to create one instance of CarRental, give it a customer name, a make and the
first key, print customer, key and daily fee.

I should just setup stuff like String name = Joe, String model = Tarus, char key = A.

Error Im getting.

CarRentalTester.java:28: cannot find symbol
symbol : constructor CarRental()
location: class CarRental
CarRental myCar1 = new CarRental();
^
1 error
Thank you all again for everything.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By editing your original posts, you have committed a JavaRanch faux pas. I'm sure the Java Ranch ambassadors can explain it better but now the replies to the original post don't make sense. Anyone reading the thread will not understand the responses. IMO, editing should be reserved to fixing spelling or grammatical errors or, for example, when I edited my response because I missed a "]" in the response and therefore my reply became part of the quote to which I was responding.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:By editing your original posts, you have committed a JavaRanch faux pas. I'm sure the Java Ranch ambassadors can explain it better but now the replies to the original post don't make sense. Anyone reading the thread will not understand the responses. IMO, editing should be reserved to fixing spelling or grammatical errors or, for example, when I edited my response because I missed a "]" in the response and therefore my reply became part of the quote to which I was responding.



oh my apologies. wont happen again. I figured fixing my original code, would be better than spamming.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

*** double fee = Math.round(((daily*ratingKey)*100.0))/100.0; ***is not working correctly any ideas?
It should work, I did it by hand and it comes out. When I do it like so, daily(92.76) * 100, then * by the factor say 1.6 = 14841.6/ by 100= 148.416 and then round would be a 2 digit number, but my java formula no matter how I write it wont come out like this

What is the actual outcome? You don't specify what is wrong.

So, You just have to create one instance of CarRental, give it a customer name, a make and the
first key, print customer, key and daily fee.
CarRentalTester.java:28: cannot find symbol
symbol : constructor CarRental()
location: class CarRental
CarRental myCar1 = new CarRental();
^
1 error]

CarRental() is called a default constructor (no parameters). You don't have one of these. You have a constructor that requires three parameters:So you need to pass the three values to the constructor.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:

*** double fee = Math.round(((daily*ratingKey)*100.0))/100.0; ***is not working correctly any ideas?
It should work, I did it by hand and it comes out. When I do it like so, daily(92.76) * 100, then * by the factor say 1.6 = 14841.6/ by 100= 148.416 and then round would be a 2 digit number, but my java formula no matter how I write it wont come out like this

What is the actual outcome? You don't specify what is wrong.


So, You just have to create one instance of CarRental, give it a customer name, a make and the
first key, print customer, key and daily fee.
CarRentalTester.java:28: cannot find symbol
symbol : constructor CarRental()
location: class CarRental
CarRental myCar1 = new CarRental();
^
1 error]

CarRental() is called a default constructor (no parameters). You don't have one of these. You have a constructor that requires three parameters:So you need to pass the three values to the constructor.


Ive been trying to pass in CarRental( Chris, Tarus, S) and it keeps erroring on me.

the outcome should be it giving a price for the fee of oneday of a rental based on the factor from the switch statement. When I run it it keeps coming out as 7000++.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My error


CarRentalTester.java:27: cannot find symbol
symbol : variable Chris
location: class CarRentalTester
CarRental myCar1 = new CarRental(Chris, Tarus, S);
^
CarRentalTester.java:27: cannot find symbol
symbol : variable Tarus
location: class CarRentalTester
CarRental myCar1 = new CarRental(Chris, Tarus, S);
^
CarRentalTester.java:27: cannot find symbol
symbol : variable S
location: class CarRentalTester
CarRental myCar1 = new CarRental(Chris, Tarus, S);
^
CarRentalTester.java:27: internal error; cannot instantiate CarRental(java.lang.String,java.lang.String,char) at CarRental to ()
CarRental myCar1 = new CarRental(Chris, Tarus, S);
^
4 errors

my code



 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are Chris and Tarus variables? Or are you trying to pass in string literals? What book are you using? What web resources are you looking at to understand basic syntactical questions?
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Are Chris and Tarus variables? Or are you trying to pass in string literals? What book are you using? What web resources are you looking at to understand basic syntactical questions?



BIG JAVA, 3rd Edition by Cay Horstmann, Wiley 2008 and the API


I've already tried assigning Chris as a String and Tarus as a String and S as a Char. But it errors out.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a newbie I am very interested in other newbie's problems hoping to avoid the pits they fall into...

So please bear with me if my remarks sound a bit amateurish.

Your code without the comments is:



Now, when I test this code, I cannot compile it. And even being a newbie I seem to understand my compiler wholeheartedly. You say in your first actual code line (line 9) that you want Java to create an object of the class CarRental and then assign the reference to it to a variable named myCar1. Where is this class "CarRental" defined? It is not part of the standard library and I do not find any definition of this class in your code. Do you have another java-file where this class is defined? You seemed to have it in some posts above.

So the first thing I would add would be something like this:



This will reduce your compiler errors a bit. But not enough. Now the whining piece of compiler code exaggerates itself about Chris, Taurus and S.

Chris, Taurus and S are identifiers for something, or so it seems. I am suspecting you mean



That would make a bit of sense, because then you pass 2 Strings and one character to the (non-standard) constructor of your newly defined class CarRental which hopefully does what you want. If I misunderstood you and you really wanted to pass 3 identifiers of something, you should advise the Java compiler which kind of "something" you had in mind. Neither the compiler nor I can imagine what your thought processes were when you wrote down this line of code (that sounds kind of harsh by reading it the second time, but it is not meant like this - I make this kind of mistake quite often myself).

Now there remains the case of the missing method. You want to compute the daily fee with the aptly named method "computeDailyFee()". You should therefore define this method in your class declaration, as you did a while back in another java-File (or is it the same? I am a bit confused here). In my class declaration I inserted such a method, much simpler than your old method. Now your program compiles on my computer but it does nothing or at least nothing which would make any sense...

I hope though that these remarks from a newbie perspective help a bit.

And if you have time I would advise you to work through a book like "Head First Java". I read it and I liked it very much. It has a limited approach (teaching the basics) but in these limits it is great.
 
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are also making the mistake of writing too much code at a time. Write one method at a time, compiling your code and testing the method every time. That way you only have about 5-10 new lines to add errors, and errors become much easier to sort out.
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are also making the mistake of writing too much code at a time. Write one method at a time, compiling your code and testing the method every time. That way you only have about 5-10 new lines to add errors, and errors become much easier to sort out.



Will do from now on.

thanks for everything everyone. I finished, I was tired lastnight, so stupid mistakes started.
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Staves wrote: . . . I was tired . . .

Shows how dangerous it is to try hard programming when tired. That is why a good company will avoid lots of overtime.
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic