4 Replies - 801 Views - Last Post: 17 November 2008 - 07:43 AM Rate Topic: -----

#1 Smirre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-November 08

[Homework] Need help with a calculator project...

Post icon  Posted 17 November 2008 - 04:29 AM

Hi all, I've just registered with dreamincode and it looks fantastic, and this is my first post.
I please need help with a 'Calculator Project' I need to do, I've created methods ' octalEquivalent(); & hexadecimalEquivalent(); ' which must take the numbers that the user entered in the console and output their equivalent octal and hex numbers e.g. num1 = 17 and num2 = 33; num1 and num2 is of type String.

I have posted all my code that I've done so far so that you can see why the num's are of type String...

Here is the project specifications: (for interest sake)

"""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."""


The methods for octal and hex:

			   public static String octalEquivalent1() {
		String octal1 = Integer.toOctalString(num1);
		return octal1;
	}
	

	public static String octalEquivalent2() {
		String octal2 = Integer.toOctalString(num2);
		return octal2;
	}
	
	public static String hexadecimalEquivalent1() {
		String hex1 = Integer.toHexString(num1);
		return hex1;
	}
	
	public static String hexadecimalEquivalent2() {
		String hex2 = Integer.toHexString(num2);
		return hex2;
	}



the error messages I'm getting is " The method toOctalString(int) in the type Integer is not applicable for the arguments (String) " & " The method toHexString(int) in the type Integer is not applicable for the arguments (String) ", I know the problem lies within the error messages, but I don't know how to correct this.

Can anybody please help me.

Here is the code that I've done so far:

import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;

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 static String octalEquivalent1() {
		String octal1 = Integer.toOctalString(num1);
		return octal1;
	}
	

	public static String octalEquivalent2() {
		String octal2 = Integer.toOctalString(num2);
		return octal2;
	}
	
	public static String hexadecimalEquivalent1() {
		String hex1 = Integer.toHexString(num1);
		return hex1;
	}
	
	public static String hexadecimalEquivalent2() {
		String hex2 = Integer.toHexString(num2);
		return hex2;
	}
	
	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));
					octalEquivalent2();
					System.out.println("The octal equivalent of " + num2 + " is: " + decimals.format(answer));
					hexadecimalEquivalent1();
					System.out.println("The hexadecimal equivalent of " + num1 + " 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("");
			
		}
}



If you would be so kind as to test my code for yourself, and point out any other mistakes or anything that I can better on, I would appreciate it.

All your help would be much appreciated.
Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: [Homework] Need help with a calculator project...

#2 Hurshai  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 14-September 08

Re: [Homework] Need help with a calculator project...

Posted 17 November 2008 - 04:50 AM

Your trying to pass a string as an argument instead of an int. You'll have to parse the value of the string and then pass it in.

Maybe something more along the lines of this:


public static String octalEquivalent2() {
		int iNum2 = Integer.parseInt(num2);
		String octal2 = Integer.toOctalString(iNum2);
		return octal2;
	}



Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: [Homework] Need help with a calculator project...

Posted 17 November 2008 - 04:56 AM

I would give the method a parameter, e.g.

public static String toOctal(int n) {
		return Integer.toOctalString(n);
}


Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: [Homework] Need help with a calculator project...

Posted 17 November 2008 - 06:10 AM

As a general rule, don't forget that you'll largely need to treat the numbers as numbers, not as String. The only time you need to work with String is at the point of collection of values from the display and at the point of delivery of values to the display
Was This Post Helpful? 0
  • +
  • -

#5 Smirre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-November 08

Re: [Homework] Need help with a calculator project...

Posted 17 November 2008 - 07:43 AM

There is another thing that I don't know how to correct, the problem is, when the user comes to the end of the program, the program asks the user if he/she wants to do another calculation.

I have created a method for this which is called " antoherCalculation(); " , what I want the method to do is, if the user answers ' y/Y ', it must ask the user again for a number and continue with the process as in the beginning (except for asking his/her name again!).

At the moment my program allows any input and continues on and on and on without stopping...

I want it to accept on answers ' y/Y ' or 'n/N' , when the user chooses yes the program must continue and when the user chooses no, the program must stop/exit, and display a message for the user, stating the current time, and calculate and display how long the user used the program.

Here is the code for the method ' anotherCalculation(); ' :

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;
		}



Please help.
Thanks


btw, thanks g00se I'll keep in mind of what you have said
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1