In: Computer Science
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String fName = "";
String lName = "";
double hoursWorked = 10;
double hourlyRate = 10;
double regularPay = 10;
double overtimeHours = 10;
int choice;
System.out.println("Calculator Menu");
System.out.println("1) Wage Calculator");
System.out.println("2) Coupon Calculator");
System.out.println("3) Roman Number Converter");
System.out.println("4) Exit");
System.out.println("");
System.out.println("Enter choice: ");
choice = in.nextInt();
switch(choice) {
case 1:
System.out.println("Enter first name: ");
fName = in.next();
System.out.println("Enter last name: ");
lName = in.next();
System.out.println("Enter hourly rate: ");
hourlyRate = in.nextDouble();
System.out.println("Enter hours worked: ");
hoursWorked = in.nextDouble();
System.out.printf("Name: " + fName + "," + lName);
System.out.println("");
wageCalculator(hoursWorked, overtimeHours, hourlyRate, regularPay);
break;
case 2:
double purchaseAmount;
int purchaseType;
final double autoP = 1;
final double frag = 2;
final double acc = 3;
System.out.println("Enter purchase amount: ");
purchaseAmount = in.nextDouble(); {
System.out.println("Choose purchase type: ");
System.out.println("1) Auto Parts");
System.out.println("2) Fragrances");
System.out.println("3) Accessories");
purchaseType = in.nextInt();
}
couponCalculator(purchaseType, purchaseAmount, autoP, frag, acc);
break;
case 3:
double calc_w;
/*
int length = Integer.toString(numeral).length();
System.out.println("Please enter Roman Number: ");
*/ String numeral = null;
int total = 0;
int length = numeral.length();
System.out.print("Enter roman numeral to convert: ");
numeral =in.next();
// total = convertNum(romanNum);
System.out.println("The roman numeral converted is: " + total);
total = in.nextInt();
// Method valueOf - Gives the value of the Roman numeral @param numeral a single Roman numeral
// @return the decimal value of numeral
//promp user to enter roman number
// then convert it to regular number
convertRomanNumber(total, length, numeral);
break;
case 4:
// exit program
System.out.println("Thank you for using our program. Have a great day!");
}
}
public static void wageCalculator(double hoursWorked, double overtimeHours, double hourlyRate, double regularPay) {
// TODO Auto-generated method stub
double overtimePay = 0;
if (hoursWorked > 40) {
overtimeHours = hoursWorked - 40;
System.out.println("Overtime hours: " + overtimeHours);
overtimePay = overtimeHours*(1.5*hourlyRate);
System.out.println("Overtime Pay: " + overtimePay);
System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
hoursWorked = 40;
}
regularPay = hourlyRate*hoursWorked;
System.out.println("Regular hours worked: " + hoursWorked);
System.out.println("Regular Pay: $" + regularPay);
System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
}
// add all variables to method header
public static void couponCalculator(int purchaseType, double purchaseAmount, double autoP, double frag, double acc) {
if (purchaseType == autoP) {
System.out.println("Your coupon is: $" + purchaseAmount*.10 + "(Auto Parts)");
} if (purchaseType == frag) {
System.out.println("Your coupon is: $" + purchaseAmount*.15 + "(Fragrances)");
} if (purchaseType == acc) {
System.out.println("Your coupon is: $" + purchaseAmount*.20 + "(Accessories)");
}
}
public static int convertRomanNumber(String str) {
}
return total;
}
public static int valueOf(char numeral) {
if (numeral == 'I') {
return 1;
}
if (numeral == 'V') {
return 5;
} if (numeral == 'X') {
return 10;
} if (numeral == 'L') {
return 50;
} if (numeral == 'C') {
return 100;
} if
(numeral == 'D')
return 500;
else if (numeral == 'M')
return 1000;
return numeral;
}
}
Implementation:
public static void main(String[] args) {
// TODO code application logic here
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String fName = "";
String lName = "";
double hoursWorked = 10;
double hourlyRate = 10;
double regularPay = 10;
double overtimeHours = 10;
int choice;
System.out.println("Calculator Menu");
System.out.println("1) Wage Calculator");
System.out.println("2) Coupon Calculator");
System.out.println("3) Roman Number Converter");
System.out.println("4) Exit");
System.out.println("");
System.out.println("Enter choice: ");
choice = in.nextInt();
switch(choice) {
case 1:
System.out.println("Enter first name: ");
fName = in.next();
System.out.println("Enter last name: ");
lName = in.next();
System.out.println("Enter hourly rate: ");
hourlyRate = in.nextDouble();
System.out.println("Enter hours worked: ");
hoursWorked = in.nextDouble();
System.out.printf("Name: " + fName + "," + lName);
System.out.println("");
wageCalculator(hoursWorked, overtimeHours, hourlyRate, regularPay);
break;
case 2:
double purchaseAmount;
int purchaseType;
final double autoP = 1;
final double frag = 2;
final double acc = 3;
System.out.println("Enter purchase amount: ");
purchaseAmount = in.nextDouble(); {
System.out.println("Choose purchase type: ");
System.out.println("1) Auto Parts");
System.out.println("2) Fragrances");
System.out.println("3) Accessories");
purchaseType = in.nextInt();
}
couponCalculator(purchaseType, purchaseAmount, autoP, frag, acc);
break;
case 3:
double calc_w;
/*
int length = Integer.toString(numeral).length();
System.out.println("Please enter Roman Number: ");
*/
String numeral;
int total = 0;
System.out.print("Enter roman numeral to convert: ");
numeral =in.next();
int length = numeral.length();
total = convertRomanNumber(total, length, numeral);
System.out.println("The roman numeral converted is: " + total);
// Method valueOf - Gives the value of the Roman numeral @param numeral a single Roman numeral
// @return the decimal value of numeral
//promp user to enter roman number
// then convert it to regular number
break;
case 4:
// exit program
System.out.println("Thank you for using our program. Have a great day!");
}
}
public static void wageCalculator(double hoursWorked, double overtimeHours, double hourlyRate, double regularPay) {
// TODO Auto-generated method stub
double overtimePay = 0;
if (hoursWorked > 40) {
overtimeHours = hoursWorked - 40;
System.out.println("Overtime hours: " + overtimeHours);
overtimePay = overtimeHours*(1.5*hourlyRate);
System.out.println("Overtime Pay: " + overtimePay);
System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
hoursWorked = 40;
}
regularPay = hourlyRate*hoursWorked;
System.out.println("Regular hours worked: " + hoursWorked);
System.out.println("Regular Pay: $" + regularPay);
System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
}
// add all variables to method header
public static void couponCalculator(int purchaseType, double purchaseAmount, double autoP, double frag, double acc) {
if (purchaseType == autoP) {
System.out.println("Your coupon is: $" + purchaseAmount*.10 + "(Auto Parts)");
} if (purchaseType == frag) {
System.out.println("Your coupon is: $" + purchaseAmount*.15 + "(Fragrances)");
} if (purchaseType == acc) {
System.out.println("Your coupon is: $" + purchaseAmount*.20 + "(Accessories)");
}
}
public static int convertRomanNumber(int total,int length,String str) {
for(int i=0;i<length;i++)//loop till length of string
{
int str1=valueOf(str.charAt(i));//getting value at str[i];
if(i+1<length)
{
int str2=valueOf(str.charAt(i+1));//at str[i+1]
if(str1<str2)
{
total=total+(str2-str1);
i++;
}
else if(str1>=str2)
{//if(current symbol is greater than or equal to next symbol then need to add both number otherwise subtract (str2-str1)
total=total+str1;
}
}else{
total=total+str1;//simply add last number
i++;
}
}
return total;
}
public static int valueOf(char numeral) {
if (numeral == 'I') {
return 1;
}
if (numeral == 'V') {
return 5;
} if (numeral == 'X') {
return 10;
} if (numeral == 'L') {
return 50;
} if (numeral == 'C') {
return 100;
} if
(numeral == 'D')
return 500;
else if (numeral == 'M')
return 1000;
return -1;
}
}
//you have done a pretty nice job well-done keep growing!!
//code is self-explanatory do tell me in comment if unable to understand!!