| | |
[HOMEWORK] Need help with java project
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
Need help with a java project I need to do. Here is the project specifications :
"""Create a console calculator applicaion that:
* Takes one command line argument: your name and surname. When the
program starts, display the date and time with a welcome message for the
user.
* Display all the available options to the user. Your calculator must include
the arithmetic operations as well as at least five scientific operations of the
Math class.
-Your program must also have the ability to round a number and
truncate it.
-When you multiply by 2, you should not use the '*' operator to perform the
operation.
-Your program must also be able to reverse the sign of a number.
* Include sufficient error checking in your program to ensure that the user
only enters valid input. Make use of the String; Character, and other
wrapper classes to help you.
* Your program must be able to do conversions between decimal, octal and
hex numbers.
* Make use of a menu. You should give the user the option to end the
program when entering a certain option.
* When the program exits, display a message for the user, stating the
current time, and calculate and display how long the user used your
program.
* Make use of helper classes where possible.
* Use the SDK to run your program."""
I am stuck at the moment and don't know how to correct this problem.
I have created a method ' hexadecimalEquivalent(); ' (among others...) which if the user chooses, it converts the numbers he/she provided as input to the hexadecimal equivalent, my ' octalEquivalent(); ' method works but I get an error message with my ' hexadecimalEquivalent(); ' method and it states
" Exception in thread "main" java.lang.NumberFormatException: For input string: "b"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at org.Java.Instructor.Project.hexadecimalEquivalent1(Project.java:86)
at org.Java.Instructor.Project.Calculator(Project.java:271)
at org.Java.Instructor.Project.main(Project.java:329)
"
Can anyone please help me.
Here is the code that I have done so far for my Project.class:
Your help will be much appreciated.
Thanks
"""Create a console calculator applicaion that:
* Takes one command line argument: your name and surname. When the
program starts, display the date and time with a welcome message for the
user.
* Display all the available options to the user. Your calculator must include
the arithmetic operations as well as at least five scientific operations of the
Math class.
-Your program must also have the ability to round a number and
truncate it.
-When you multiply by 2, you should not use the '*' operator to perform the
operation.
-Your program must also be able to reverse the sign of a number.
* Include sufficient error checking in your program to ensure that the user
only enters valid input. Make use of the String; Character, and other
wrapper classes to help you.
* Your program must be able to do conversions between decimal, octal and
hex numbers.
* Make use of a menu. You should give the user the option to end the
program when entering a certain option.
* When the program exits, display a message for the user, stating the
current time, and calculate and display how long the user used your
program.
* Make use of helper classes where possible.
* Use the SDK to run your program."""
I am stuck at the moment and don't know how to correct this problem.
I have created a method ' hexadecimalEquivalent(); ' (among others...) which if the user chooses, it converts the numbers he/she provided as input to the hexadecimal equivalent, my ' octalEquivalent(); ' method works but I get an error message with my ' hexadecimalEquivalent(); ' method and it states
" Exception in thread "main" java.lang.NumberFormatException: For input string: "b"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at org.Java.Instructor.Project.hexadecimalEquivalent1(Project.java:86)
at org.Java.Instructor.Project.Calculator(Project.java:271)
at org.Java.Instructor.Project.main(Project.java:329)
"
Can anyone please help me.
Here is the code that I have done so far for my Project.class:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; import java.text.*; public class Project { private static String nameSurname = ""; private static String num1 = null; private static String num2 = null; private static String choice1 = null; private static double answer = 0; private static char more; public double Add() { answer = (Double.parseDouble(num1) + Double.parseDouble(num2)); return answer; } public double Subtract() { answer = (Double.parseDouble(num1) - Double.parseDouble(num2)); return answer; } public double Multiply() { answer = (Double.parseDouble(num1) * Double.parseDouble(num2)); return answer; } public double Divide() { answer = (Double.parseDouble(num1) / Double.parseDouble(num2)); return answer; } public double Modulus() { answer = (Double.parseDouble(num1) % Double.parseDouble(num2)); return answer; } public double maximumValue() { answer = (Math.max(Double.parseDouble(num1), Double.parseDouble(num2))); return answer; } public double minimumValue() { answer = (Math.min(Double.parseDouble(num1), Double.parseDouble(num2))); return answer; } public double absoluteNumber1() { answer = (Math.abs(Double.parseDouble(num1))); return answer; } public double absoluteNumber2() { answer = (Math.abs(Double.parseDouble(num2))); return answer; } public double Squareroot1() { answer = (Math.sqrt(Double.parseDouble(num1))); return answer; } public double Squareroot2() { answer = (Math.sqrt(Double.parseDouble(num2))); return answer; } public double octalEquivalent1() { long n1 = Long.parseLong(num1); answer = Long.parseLong(Long.toOctalString(n1)); return answer; } public double octalEquivalent2() { long n2 = Long.parseLong(num2); answer = Long.parseLong(Long.toHexString(n2)); return answer; } public double hexadecimalEquivalent1() { long n1 = Long.parseLong(num1); answer = Long.parseLong(Long.toHexString(n1)); return answer; } public double hexadecimalEquivalent2() { long n2 = Long.parseLong(num2); answer = Long.parseLong(Long.toHexString(n2)); return answer; } public double Round1() { answer = Math.round(Double.parseDouble(num1)); return answer; } public double Round2() { answer = Math.round(Double.parseDouble(num2)); return answer; } SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy"); Date now = new Date(); SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a"); Date now2 = new Date(); DecimalFormat decimals = new DecimalFormat("#0.00"); public String insertNameAndSurname() throws IOException{ boolean inputCorrect = false; while (inputCorrect == false) { while (nameSurname == null || nameSurname.length() == 0) { for (int i = 0; i < nameSurname.length(); i++) { if ((nameSurname.charAt(i) > 'a') && (nameSurname.charAt(i) < 'Z')){ inputCorrect = true; } else{ inputCorrect = false; break; } } try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Please enter your name and surname: "); nameSurname = inStream.readLine(); inputCorrect = true; }catch (IOException ex) { System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :"); inputCorrect = false; } } System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now)); System.out.println("and the time is now exactly " + format2.format(now2) + "."); } return nameSurname; } public String inputNumber1() throws IOException { boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nPlease enter a number you want to do a calculation with and hit <ENTER>: "); num1 = br.readLine(); double number1 = Double.parseDouble(num1); System.out.println("\nThe number you have entered is: " + number1); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("\nYou did not enter a valid number: " + "\""+ num1 + "\" is not a number!!"); inputCorrect = false; } } return num1; } public String calculatorChoice() throws IOException { System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: "); System.out.println("\n*********************************************"); System.out.println("---------------------------------------------"); System.out.println("Please select an option from the list below: "); System.out.println("---------------------------------------------"); System.out.println("1 - Add"); System.out.println("2 - Subtract"); System.out.println("3 - Multiply"); System.out.println("4 - Divide (remainder included)"); System.out.println("5 - Maximum and minimum value of two numbers"); System.out.println("6 - Squareroot"); System.out.println("7 - Absolute value of numbers"); System.out.println("8 - Octal and Hexadecimal equivalent of numbers"); System.out.println("9 - Round numbers"); System.out.println("0 - Exit program"); System.out.println("**********************************************"); boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Please enter your option and hit <ENTER>: "); choice1 = inStream.readLine(); int c1 = Integer.parseInt(choice1); System.out.println("\nYou have entered choice number: " + c1); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("You did not enter a valid choice number: " + "\""+ choice1 + "\" is not in the list!!"); inputCorrect = false; } } return choice1; } public String inputNumber2() throws IOException { boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nPlease enter another number you want to do the calculation with and hit <ENTER>: "); num2 = br2.readLine(); double n2 = Double.parseDouble(num2); System.out.println("\nThe second number you have entered is: " + n2); System.out.println("\nYour numbers are: " + num1 + " and " + num2); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("You did not enter a valid number: " + "\""+ num2 + "\" is not a number!!"); inputCorrect = false; } } return num2; } public int Calculator() { int choice2 = (int) Double.parseDouble(choice1); switch (choice2) { case 1 : Add(); System.out.print("The answer of " + num1 + " + " + num2 + " is: " + decimals.format(answer)); break; case 2 : Subtract(); System.out.print("The answer of " + num1 + " - " + num2 + " is: " + decimals.format(answer)); break; case 3 : Multiply(); System.out.print("The answer of " + num1 + " * " + num2 + " is: " + decimals.format(answer)); break; case 4 : Divide(); System.out.print("The answer of " + num1 + " / " + num2 + " is: " + decimals.format(answer)); Modulus(); System.out.print(" and the remainder is " + decimals.format(answer)); break; case 5 : maximumValue(); System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer)); minimumValue(); System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer)); break; case 6 : Squareroot1(); System.out.println("The squareroot of value " + num1 + " is: " + decimals.format(answer)); Squareroot2(); System.out.println("The squareroot of value " + num2 + " is: " + decimals.format(answer)); break; case 7 : absoluteNumber1(); System.out.println("The absolute number of " + num1 + " is: " + decimals.format(answer)); absoluteNumber2(); System.out.println("The absolute number of " + num2 + " is: " + decimals.format(answer)); break; case 8 : octalEquivalent1(); System.out.println("The octal equivalent of " + num1 + " is: " + decimals.format(answer)); hexadecimalEquivalent1(); System.out.println("The hexadecimal equivalent of " + num1 + " is: " + decimals.format(answer)); octalEquivalent2(); System.out.println("\nThe octal equivalent of " + num2 + " is: " + decimals.format(answer)); hexadecimalEquivalent2(); System.out.println("The hexadecimal equivalent of " + num2 + " is: " + decimals.format(answer)); break; case 9 : Round1(); System.out.println("The rounded number of " + num1 + " is: " + decimals.format(answer)); Round2(); System.out.println("The rounded number of " + num2 + " is: " + decimals.format(answer)); break; case 0 : if (choice2 == 0) { System.exit(1); } break; } return choice2; } public char anotherCalculation() throws IOException { try { System.out.print("\nWould you like to do another calculation? Y/N"); more = (char) System.in.read(); do { inputNumber1(); System.out.println(""); calculatorChoice(); System.out.println(""); inputNumber2(); System.out.println(""); Calculator(); System.out.println(""); anotherCalculation(); System.out.println(""); } while((more == 'y') | (more == 'Y')); System.exit(0); } catch (IOException ex){ System.exit(0); } return more; } public static void main(String[] args) throws IOException { Project p1 = new Project(); p1.insertNameAndSurname(); System.out.println(""); p1.inputNumber1(); System.out.println(""); p1.calculatorChoice(); System.out.println(""); p1.inputNumber2(); System.out.println(""); p1.Calculator(); System.out.println(""); p1.anotherCalculation(); System.out.println(""); } }
Your help will be much appreciated.
Thanks
How is Long.toHexString working for octal ? where you are supposed to convert a string in hex format ?
![]() |
Similar Threads
- Guidelines before posting (DaniWeb Community Feedback)
- Java Project (Java)
- my project is IDE for java (Java)
- homework(easy): Java GUI/Netbeans using java.util.LinkedList (Java)
- About a Java project (Java)
- Homework Help (Java)
- Homework Help (Community Introductions)
- Homework Help (Java)
Other Threads in the Java Forum
- Previous Thread: JAVA mobile Application (i need some help)
- Next Thread: java very basic input problem
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






