[HOMEWORK] Need help with java project

Reply

Join Date: Nov 2008
Posts: 1
Reputation: Smirre is an unknown quantity at this point 
Solved Threads: 0
Smirre Smirre is offline Offline
Newbie Poster

[HOMEWORK] Need help with java project

 
0
  #1
Nov 16th, 2008
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:

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4.  
  5. public class Project {
  6.  
  7.  
  8. private static String nameSurname = "";
  9. private static String num1 = null;
  10. private static String num2 = null;
  11. private static String choice1 = null;
  12. private static double answer = 0;
  13. private static char more;
  14.  
  15. public double Add() {
  16. answer = (Double.parseDouble(num1) + Double.parseDouble(num2));
  17. return answer;
  18. }
  19.  
  20. public double Subtract() {
  21. answer = (Double.parseDouble(num1) - Double.parseDouble(num2));
  22. return answer;
  23. }
  24.  
  25. public double Multiply() {
  26. answer = (Double.parseDouble(num1) * Double.parseDouble(num2));
  27. return answer;
  28. }
  29.  
  30. public double Divide() {
  31. answer = (Double.parseDouble(num1) / Double.parseDouble(num2));
  32. return answer;
  33. }
  34.  
  35. public double Modulus() {
  36. answer = (Double.parseDouble(num1) % Double.parseDouble(num2));
  37. return answer;
  38. }
  39.  
  40. public double maximumValue() {
  41. answer = (Math.max(Double.parseDouble(num1), Double.parseDouble(num2)));
  42. return answer;
  43. }
  44.  
  45. public double minimumValue() {
  46. answer = (Math.min(Double.parseDouble(num1), Double.parseDouble(num2)));
  47. return answer;
  48. }
  49.  
  50. public double absoluteNumber1() {
  51. answer = (Math.abs(Double.parseDouble(num1)));
  52. return answer;
  53. }
  54.  
  55. public double absoluteNumber2() {
  56. answer = (Math.abs(Double.parseDouble(num2)));
  57. return answer;
  58. }
  59.  
  60. public double Squareroot1() {
  61. answer = (Math.sqrt(Double.parseDouble(num1)));
  62. return answer;
  63. }
  64.  
  65. public double Squareroot2() {
  66. answer = (Math.sqrt(Double.parseDouble(num2)));
  67. return answer;
  68. }
  69.  
  70. public double octalEquivalent1() {
  71. long n1 = Long.parseLong(num1);
  72. answer = Long.parseLong(Long.toOctalString(n1));
  73. return answer;
  74. }
  75.  
  76. public double octalEquivalent2() {
  77. long n2 = Long.parseLong(num2);
  78. answer = Long.parseLong(Long.toHexString(n2));
  79. return answer;
  80. }
  81.  
  82. public double hexadecimalEquivalent1() {
  83. long n1 = Long.parseLong(num1);
  84. answer = Long.parseLong(Long.toHexString(n1));
  85. return answer;
  86. }
  87.  
  88. public double hexadecimalEquivalent2() {
  89. long n2 = Long.parseLong(num2);
  90. answer = Long.parseLong(Long.toHexString(n2));
  91. return answer;
  92. }
  93.  
  94. public double Round1() {
  95. answer = Math.round(Double.parseDouble(num1));
  96. return answer;
  97. }
  98.  
  99. public double Round2() {
  100. answer = Math.round(Double.parseDouble(num2));
  101. return answer;
  102. }
  103.  
  104.  
  105. SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy");
  106. Date now = new Date();
  107.  
  108. SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a");
  109. Date now2 = new Date();
  110.  
  111. DecimalFormat decimals = new DecimalFormat("#0.00");
  112.  
  113.  
  114.  
  115.  
  116. public String insertNameAndSurname() throws IOException{
  117. boolean inputCorrect = false;
  118. while (inputCorrect == false) {
  119. while (nameSurname == null || nameSurname.length() == 0) {
  120.  
  121. for (int i = 0; i < nameSurname.length(); i++) {
  122. if ((nameSurname.charAt(i) > 'a') && (nameSurname.charAt(i) < 'Z')){
  123. inputCorrect = true;
  124. }
  125. else{
  126. inputCorrect = false;
  127. break;
  128. }
  129. }
  130.  
  131. try {
  132. BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
  133. System.out.print("Please enter your name and surname: ");
  134. nameSurname = inStream.readLine();
  135. inputCorrect = true;
  136. }catch (IOException ex) {
  137. System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :");
  138. inputCorrect = false;
  139. }
  140. }
  141. System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now));
  142. System.out.println("and the time is now exactly " + format2.format(now2) + ".");
  143. }
  144. return nameSurname;
  145. }
  146.  
  147.  
  148.  
  149. public String inputNumber1() throws IOException {
  150. boolean inputCorrect = false;
  151. while (inputCorrect == false) {
  152. try {
  153. BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
  154. System.out.print("\nPlease enter a number you want to do a calculation with and hit <ENTER>: ");
  155. num1 = br.readLine();
  156. double number1 = Double.parseDouble(num1);
  157. System.out.println("\nThe number you have entered is: " + number1);
  158. inputCorrect = true;
  159. } catch (NumberFormatException nfe) {
  160. System.out.println("\nYou did not enter a valid number: " + "\""+ num1 + "\" is not a number!!");
  161. inputCorrect = false;
  162. }
  163. }
  164. return num1;
  165. }
  166.  
  167.  
  168. public String calculatorChoice() throws IOException {
  169. System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: ");
  170.  
  171. System.out.println("\n*********************************************");
  172. System.out.println("---------------------------------------------");
  173. System.out.println("Please select an option from the list below: ");
  174. System.out.println("---------------------------------------------");
  175. System.out.println("1 - Add");
  176. System.out.println("2 - Subtract");
  177. System.out.println("3 - Multiply");
  178. System.out.println("4 - Divide (remainder included)");
  179. System.out.println("5 - Maximum and minimum value of two numbers");
  180. System.out.println("6 - Squareroot");
  181. System.out.println("7 - Absolute value of numbers");
  182. System.out.println("8 - Octal and Hexadecimal equivalent of numbers");
  183. System.out.println("9 - Round numbers");
  184. System.out.println("0 - Exit program");
  185. System.out.println("**********************************************");
  186.  
  187. boolean inputCorrect = false;
  188. while (inputCorrect == false) {
  189. try {
  190. BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
  191. System.out.print("Please enter your option and hit <ENTER>: ");
  192. choice1 = inStream.readLine();
  193. int c1 = Integer.parseInt(choice1);
  194. System.out.println("\nYou have entered choice number: " + c1);
  195. inputCorrect = true;
  196. } catch (NumberFormatException nfe) {
  197. System.out.println("You did not enter a valid choice number: " + "\""+ choice1 + "\" is not in the list!!");
  198. inputCorrect = false;
  199. }
  200. }
  201. return choice1;
  202. }
  203.  
  204.  
  205. public String inputNumber2() throws IOException {
  206. boolean inputCorrect = false;
  207. while (inputCorrect == false) {
  208. try {
  209. BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in));
  210. System.out.print("\nPlease enter another number you want to do the calculation with and hit <ENTER>: ");
  211. num2 = br2.readLine();
  212. double n2 = Double.parseDouble(num2);
  213. System.out.println("\nThe second number you have entered is: " + n2);
  214. System.out.println("\nYour numbers are: " + num1 + " and " + num2);
  215. inputCorrect = true;
  216. } catch (NumberFormatException nfe) {
  217. System.out.println("You did not enter a valid number: " + "\""+ num2 + "\" is not a number!!");
  218. inputCorrect = false;
  219. }
  220. }
  221. return num2;
  222. }
  223.  
  224.  
  225. public int Calculator() {
  226. int choice2 = (int) Double.parseDouble(choice1);
  227. switch (choice2) {
  228. case 1 :
  229. Add();
  230. System.out.print("The answer of " + num1 + " + " + num2 + " is: " + decimals.format(answer));
  231. break;
  232. case 2 :
  233. Subtract();
  234. System.out.print("The answer of " + num1 + " - " + num2 + " is: " + decimals.format(answer));
  235. break;
  236. case 3 :
  237. Multiply();
  238. System.out.print("The answer of " + num1 + " * " + num2 + " is: " + decimals.format(answer));
  239. break;
  240. case 4 :
  241. Divide();
  242. System.out.print("The answer of " + num1 + " / " + num2 + " is: " + decimals.format(answer));
  243. Modulus();
  244. System.out.print(" and the remainder is " + decimals.format(answer));
  245. break;
  246. case 5 :
  247. maximumValue();
  248. System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer));
  249. minimumValue();
  250. System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer));
  251. break;
  252. case 6 :
  253. Squareroot1();
  254. System.out.println("The squareroot of value " + num1 + " is: " + decimals.format(answer));
  255. Squareroot2();
  256. System.out.println("The squareroot of value " + num2 + " is: " + decimals.format(answer));
  257. break;
  258. case 7 :
  259. absoluteNumber1();
  260. System.out.println("The absolute number of " + num1 + " is: " + decimals.format(answer));
  261. absoluteNumber2();
  262. System.out.println("The absolute number of " + num2 + " is: " + decimals.format(answer));
  263. break;
  264. case 8 :
  265. octalEquivalent1();
  266. System.out.println("The octal equivalent of " + num1 + " is: " + decimals.format(answer));
  267. hexadecimalEquivalent1();
  268. System.out.println("The hexadecimal equivalent of " + num1 + " is: " + decimals.format(answer));
  269. octalEquivalent2();
  270. System.out.println("\nThe octal equivalent of " + num2 + " is: " + decimals.format(answer));
  271. hexadecimalEquivalent2();
  272. System.out.println("The hexadecimal equivalent of " + num2 + " is: " + decimals.format(answer));
  273. break;
  274. case 9 :
  275. Round1();
  276. System.out.println("The rounded number of " + num1 + " is: " + decimals.format(answer));
  277. Round2();
  278. System.out.println("The rounded number of " + num2 + " is: " + decimals.format(answer));
  279. break;
  280. case 0 :
  281. if (choice2 == 0) {
  282. System.exit(1);
  283. }
  284. break;
  285. }
  286. return choice2;
  287. }
  288.  
  289.  
  290. public char anotherCalculation() throws IOException {
  291. try {
  292. System.out.print("\nWould you like to do another calculation? Y/N");
  293. more = (char) System.in.read();
  294. do {
  295. inputNumber1();
  296. System.out.println("");
  297. calculatorChoice();
  298. System.out.println("");
  299. inputNumber2();
  300. System.out.println("");
  301. Calculator();
  302. System.out.println("");
  303. anotherCalculation();
  304. System.out.println("");
  305. } while((more == 'y') | (more == 'Y'));
  306. System.exit(0);
  307. } catch (IOException ex){
  308. System.exit(0);
  309. }
  310. return more;
  311. }
  312.  
  313.  
  314. public static void main(String[] args) throws IOException {
  315. Project p1 = new Project();
  316.  
  317. p1.insertNameAndSurname();
  318. System.out.println("");
  319. p1.inputNumber1();
  320. System.out.println("");
  321. p1.calculatorChoice();
  322. System.out.println("");
  323. p1.inputNumber2();
  324. System.out.println("");
  325. p1.Calculator();
  326. System.out.println("");
  327. p1.anotherCalculation();
  328. System.out.println("");
  329.  
  330. }
  331. }

Your help will be much appreciated.
Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,810
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: [HOMEWORK] Need help with java project

 
0
  #2
Nov 17th, 2008
How is Long.toHexString working for octal ? where you are supposed to convert a string in hex format ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC