Advertisements

Help on this code please

Postby ryndgmn » Sun Feb 10, 2013 8:14 am

Offline
ryndgmn
Posts: 1
Joined: Sun Feb 10, 2013 7:57 am
Hi people, especially to programming experts or programmers. Could you please help me solve the Java code below. It's a practice fom the Java programming book that I'm currently studying but I've been having a hard time completing it. Could you please advise me on what my errors are and please provide me tips on how to effectively create Java user-defined methods. Actually as of today, I managed to cut it down to three errors na lang with the variables HOURS, TUITION, & FEES not being recognized or not being found. I'm using the latest Java SDK. Here's the Java code with comments as instructions on how to create the program:

/*
Program Name: Tuition
Programmer: Ryan L. Daguman
Date: November 30, 2012
Introduction: A small proprietary school that offers distance-learning courses would like an application that calculates total tuition and fees for their students. Users will input the number of hours and the program should calculate the total cost. Cost per credit hour for full time (greater than 15 hours) is $44.50 per credit hour; 15 hours or less costs $50.00 per credit hour.
*/

import java.io.*;

/*Create a header for the public class, Tuition, followed by an opening brace.*/
public class Tuition
{

/*Enter the standard method header, which throwns an IOException. Type the opening brace for the main header.*/
public static void main (String[] args) throws IOException
{

/*Declare an int identifier, hours. Declare double identifiers for fees, rates, and tuition.*/
int hours;
double fees, rate, tuition;

//call the user-defined methods
displayWelcome();
hours = getHours();
rate = getRate(hours);
tuition = calcTuition(hours, rate);
fees = calcFees(tuition);
displayTotal(tuition + fees);
}

/*For this method, type code statements to display a welcome message.*/
public static void displayWelcome()
{
System.out.println("\tWelcome to the tuition fee calculator program. Please utilize this program that will compute your total fees at our school.");
}

/*Construct the second user-defined method. Construct an instance of the BufferedReader. Declare strHours as String and hours as an int, setting hours to initial value of zero. Display a prompt that allows the user to enter a string value, strHours, for the total number of hours. Parse that value into the integer value, hours. This method should include a try and catch block for non-integer input. This method will return the int, hours to main.
*/
public static int getHours() throws IOException
{
try
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
String strHours;
int hours = 0;
System.out.println("\tPlease enter the total number of hours.");
strHours = dataIn.readLine();
hours = Integer.parseInt(strHours);
}
catch(NumberFormatException errRyan)
{
System.out.println("\tYour input was not a valid entry.");
System.out.println("\tPlease enter only numbers.");
}
return hours;
}

/*For the third user-defined method, include an if statement for hours greater than 15, which will calculate a rate per credit hour. This method will return the double, rate to main.*/
public static double getRate(int hours)
{
double rate;
if (hours >=15)

rate = 44.50;

else

rate = 50;

return rate;
}

/*For the fourth user-defined method, code statements to accept two values, multiply them, and reurn a double value, tuition to main.*/
public static double calcTuition(int hours, double rate)
{
double tuition = hours * rate;
return tuition;
}

/*For the fifth user-defined method, code statements to accept the double value, tuition, multiply it by .08, and then return a double value, fees to main.*/
public static double calcFees(double tuition)
{
double fees = tuition * .08;
return fees;
}

/*For the last user-defined method, use the Sstem.out.println method to display the avlaue pased by adding tuiton and fees, along with a closing message.*/
public static void displayTotal(double total)
{
System.out.println("\tYour total fees are " + "$" + (tuition + fees));
System.out.println("\tPlease consider paying in full. Thank you.");
}
}

/*Please Java prorammer experts out there, help me out. Thank you.*/

Re: Help on this code please

Postby ajitpaswan » Fri May 31, 2013 7:23 am

Offline
ajitpaswan
Posts: 11
Joined: Mon Dec 24, 2012 2:41 am
Location: India
Sorry for the late reply.

/*
Program Name: Tuition
Programmer: Ryan L. Daguman
Date: November 30, 2012
Introduction: A small proprietary school that offers distance-learning courses would like an application that calculates total tuition and fees for their students. Users will input the number of hours and the program should calculate the total cost. Cost per credit hour for full time (greater than 15 hours) is $44.50 per credit hour; 15 hours or less costs $50.00 per credit hour.
*/

import java.io.*;

/*Create a header for the public class, Tuition, followed by an opening brace.*/
public class demo
{

/*Enter the standard method header, which throwns an IOException. Type the opening brace for the main header.*/
public static void main (String[] args) throws IOException
{

/*Declare an int identifier, hours. Declare double identifiers for fees, rates, and tuition.*/
int hours;
double fees, rate, tuition;

//call the user-defined methods
displayWelcome();
hours = getHours();
rate = getRate(hours);
tuition = calcTuition(hours, rate);
fees = calcFees(tuition);
displayTotal(tuition + fees);
}

/*For this method, type code statements to display a welcome message.*/
public static void displayWelcome()
{
System.out.println("\tWelcome to the tuition fee calculator program. Please utilize this program that will compute your total fees at our school.");
}

/*Construct the second user-defined method. Construct an instance of the BufferedReader. Declare strHours as String and hours as an int, setting hours to initial value of zero. Display a prompt that allows the user to enter a string value, strHours, for the total number of hours. Parse that value into the integer value, hours. This method should include a try and catch block for non-integer input. This method will return the int, hours to main.
*/
public static int getHours() throws IOException
{
int hours = 0;
try
{

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
String strHours;
System.out.println("\tPlease enter the total number of hours.");
strHours = dataIn.readLine();
hours = Integer.parseInt(strHours);
}
catch(NumberFormatException errRyan)
{
System.out.println("\tYour input was not a valid entry.");
System.out.println("\tPlease enter only numbers.");
}
return hours;
}

/*For the third user-defined method, include an if statement for hours greater than 15, which will calculate a rate per credit hour. This method will return the double, rate to main.*/
public static double getRate(int hours)
{
double rate;
if (hours >=15)

rate = 44.50;

else

rate = 50;

return rate;
}

/*For the fourth user-defined method, code statements to accept two values, multiply them, and reurn a double value, tuition to main.*/
public static double calcTuition(int hours, double rate)
{
double tuition = hours * rate;
return tuition;
}

/*For the fifth user-defined method, code statements to accept the double value, tuition, multiply it by .08, and then return a double value, fees to main.*/
public static double calcFees(double tuition)
{
double fees = tuition * .08;
return fees;
}

/*For the last user-defined method, use the Sstem.out.println method to display the avlaue pased by adding tuiton and fees, along with a closing message.*/
public static void displayTotal(double total)
{
System.out.println("\tYour total fees are " + "$" + (total));
System.out.println("\tPlease consider paying in full. Thank you.");
}
}

Re: Help on this code please

Postby ajitpaswan » Fri May 31, 2013 7:24 am

Offline
ajitpaswan
Posts: 11
Joined: Mon Dec 24, 2012 2:41 am
Location: India
The output is :

Welcome to the tuition fee calculator program. Please utilize this program that will compute your total fees at our school.
Please enter the total number of hours.
5
Your total fees are $270.0
Please consider paying in full. Thank you.