I am a beginner in Java. I have been learning java by myself during the last few weeks and facing some difficulties with a question i got from a collegue of mine who did java at uni level. I don't know have to work with methods and she can't remember as well. Could some please help me out ??
A telephone cost plan:
Local calls are charged at $0.50 per minute(between 9a.m and 9pm). Long distance calls (> 200 miles) are charged at $1.10 per minute((between 9a.m and 9pm). From 9p.m to 9a.m,calls are charged at half the normal rate.
I have to write a program which calculates the total cost of an arbitrary number of calls. For each call, the person using the phone needs to enter a time value (in 24hr mode), a distance (in miles), and a call length (in minutes). The program must then display the cost of that call and also adds the cost onto a cumulative total.
When the phone user has no more calls to process, a time value of -999 instead of a legal time. When the program reads this sentinel value it displays the total cost of all the processed calls and ends.
Your question is too broad - you didn't identify where you are stuck.
For instance, do you have the knowledge to write a Hello World program in Java? Do you know how to read user input? Do you understand if-then logic?
It sounds like you might not know how to break up the code into appropriate methods. If that's the case, you should just write the whole program in the main method and then ask for help on redesigning it.
Hi D, i agree with one of the members saying " do you know how to write the hello world program" Well D try to break your program into steps and then list out your steps so that we can identify where you are lost.
Give it a shot. The program sounds long but its quite simple.Thats why no direct answers are being posted.
Believe it or not, you DO know how to write methods. main is a method. it takes a parameter (a String array) that we named 'args'. it's public, so anybody can call it. it's static, so it can be called without any object being created, and it doesn't return anything (void).
so you know a lot more than you thought.
Ryan is giving you good advice. Don't start off too big. just try and get some input from the user, and echo it back out. Get that to compile, run, and work correctly before the next step. it can probably be done in about 3 lines inside your main method. Heck, you may even want to start with a program that does nothing more than prompts the user for input and then exits. i.e. it doesn't actually GET the input, you just display what the prompt should be.
do you have a good book on Java? i'd reccomend the "head first Java" book by the JavaRanch's very own Kathy Sierra and Burt Bates. Also, you should become familiar with the API, found here. It can look a little daunting at first, but it really is vital. i refer to it almost every day. it tells you all about the java classes that already exist, and everything you can do with them. Things like Strings and Integers.
Like the rest of the posters here, I'd suggest that you get a handle on these fundamentals first. You need to know how to play a C-major scale before you jump into playing a Beethoven sonata.
How are you going about teaching yourself? Are you following some online tutorials? Which ones? Have you bought a book (or two)? What books?
Whatever materials you're using, they should cover how to assign a value to a variable, and how to print that variable out. They should also cover how to read user input into a variable. If the materials you've chosen skip over this stuff, or address it in a way that's not clear, it might be time to pick some different materials. We'd be glad to offer recommendations.
And, by the way, the reason we're not just posting the code to solve your problem is because we do want to help. Guiding someone through the steps required to figure something out for themselves is the best way to do that.
Hola D, Look, there are some steps that you have to take when approaching this and any future assignments that have nothing to do with synthax and everything to do with design... Do you think profi programmers spend all of their time coding? Most of the time is spent on design... One simple and very helpfull technique is to identify the verbs and nouns in the problem description. Generally, nouns will be your classes and verbs will be the methods... Good luck on your Java journey, hope you have a lot of fun...
You can't write an effective program without knowing at least the fundamentals of the language you want to write it in. Sun's tutorial is clearly written and well-paced and will get you up and running very very quickly. In fact, I would wager that if you spent just one day diligently going through the First Cup of Java and the Learning the Java Language sections of the tutorial, you'd be able to make great progress. At that point, you'd have the ability to try some things, see if they work, and to ask focused, direct questions that we would all happily answer.
But, you must first apply yourself to gaining at least a fundamental understanding of Java.
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)