Google Answers Logo
View Question
 
Q: Problem Solving with Java ( Answered,   0 Comments )
Question  
Subject: Problem Solving with Java
Category: Computers > Programming
Asked by: math01-ga
List Price: $5.00
Posted: 10 Dec 2002 01:30 PST
Expires: 09 Jan 2003 01:30 PST
Question ID: 122305
Description

Write a Java program to compute the number of years left on a person's
driver license before it needs to be renewed. Assume that the state
requires that it be renewed every five years. Your program should
prompt the user for a name, date of last renewal and the current year.
It should then display the number of years left in the license. The
output would look something like:

John Doe last renewed his license in 1999.
John Doe must renew his license in 2004.
Number of years left in this license is 2. 

Decide what variables need to be used. What should be the types of
these variables? How can you compute the date of next renewal? How do
you determine the number of years left in the current license?

Procedure

Determine what variables to use. 

Identify the data types of these variables. 
Determine how you can compute the date of next renewal 
Determine how you can compute the number of years left in the current
license.
Answer  
Subject: Re: Problem Solving with Java
Answered By: seizer-ga on 10 Dec 2002 05:15 PST
 
Hello, mhat01.

I've completed your java code, and it is available here:

http://xult.org/math01/License.java

It should be fairly self explanatory, as I've included useful comments
throughout. If anything's unclear though, please request clarification
before you rate this answer.

Good luck,

--seizer-ga

Request for Answer Clarification by math01-ga on 10 Dec 2002 06:08 PST
Can you please explain the following lines:

import java.io.*;

catch (io exception e)

public static String getInput(String question) throws IOException {
		// Display the question to the user
		System.out.print(question);
		// obtain their input
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		// return that input
		return in.readLine();

Request for Answer Clarification by math01-ga on 10 Dec 2002 06:15 PST
Also try!

Clarification of Answer by seizer-ga on 10 Dec 2002 06:19 PST
Certainly!

---
import java.io.*;

This line tells the compiler to include the standard Input and Output
classes, because we need to take input from the keyboard in the
program.
---

---
catch (IOException e) 

This line means that we're able to catch an error, if there's a
problem with the Input and Output. So basically, if there's a keyboard
problem. It's good to do this, and Java insists. The try/catch
construct works like this:

try {
 some things to try go here.
} catch (particular kinds of errors which might crop up while we're
trying the things above) {
 things to do if there is an error
}

---

---
public static String getInput(String question) throws IOException { 
  // Display the question to the user 
  System.out.print(question); 
  // obtain their input 
  BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
  // return that input 
  return in.readLine();
}

This is a method which takes in a question to ask on the screen, and
returns a String object which contains the response which is typed in.
So line by line, it:

1) Prints the question on the screen
2) Reads in everything that is then typed on the keyboard, until the
user presses Enter
3) Returns that string to the place where it was called
---

Hope this helps!

--seizer-ga

Request for Answer Clarification by math01-ga on 10 Dec 2002 06:41 PST
Could I also use swing.* as well as JOptionPane.showInputDialog in this program?

Clarification of Answer by seizer-ga on 10 Dec 2002 07:18 PST
Hello, math01.

I'm not sure I understand your need for Swing. Swing is a group of
graphical classes designed for windowed programming. The core code is
certainly able to be slotted into a windowed program, of course.

If you're interested in graphical programming, your best approach
might be to post a fresh question, detailing additional requirements
or questions you may have. This will allow all the researchers a
chance to answer it.

Regards,

--seizer-ga

Request for Answer Clarification by math01-ga on 11 Dec 2002 01:11 PST
Hi seizer-ga,

Ran program and got error message:
Error: Method readline() not found in the class java.io.BufferedReader.
ProgLicense.java line 44            return in.readline();

Request for Answer Clarification by math01-ga on 11 Dec 2002 01:30 PST
Also getting error message:

Exception in thread "main" java.lang.NoClassDefFoundError: ProgLicense

Clarification of Answer by seizer-ga on 11 Dec 2002 03:57 PST
Hello math01.

The file *must* be called License.java, or the program will not work.
This is the same for all Java programs: the filename must be the same
as the class name.

As to the other error, you've got "readline". It should be "readLine".

Hope this helps!

--seizer-ga
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy