Results 1 to 8 of 8

Thread: Java Project

  1. #1
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Exclamation Java Project

    Hi everyone, I just registered with Java Forums and it looks fantastic. I just started studying java and I please need your help with a project I need to hand in.

    The project I need to do is: (everything inside the quotation marks)...

    """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 have already started to code this project, but I'm not sure if I am on the right track...

    Here is my code so far...
    Java Code:
    import java.io.*;
    import java.util.*;
    import java.text.*;
    
    public class Project {
    	
    	
    	static String nameSurname = "";	
    	static String num = null; 
    	static String num2 = null; 
    	static int checkNumber  = 0;
    	static String choice = null;
    	
    	public static void main(String[] args) {
    		
    		SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy");
    	    Date now = new Date();
    	    
    	    SimpleDateFormat format2 = new SimpleDateFormat("hh:m a");
    	    Date now2 = new Date();
    		
    	    try {
    	    	BufferedReader inStream = new BufferedReader (
                                          new InputStreamReader(System.in)
                                      );
    	    	System.out.println("Please enter your name and surname :");
    	    	nameSurname = inStream.readLine();
    	    	System.out.println("\nA warm welcome to you " + nameSurname + " ,todays date is : " + format1.format(now));
    	    	System.out.println("and the time is now exactly " + format2.format(now2) + ".");
    	    	System.out.print("\nPlease enter a number and hit <ENTER>: ");
    	    	num = inStream.readLine();
    	    	checkNumber    = (int) Double.parseDouble(num);
    	    	System.out.println("\nThe number you have entered is: " + num);
    	    	System.out.print("\nPlease enter another number and hit <ENTER>: ");
    	    	num2 = inStream.readLine();
    	    	checkNumber    = (int) Double.parseDouble(num2);
    	    	System.out.println("\nThe number you have entered is: " + num2);
    	    	
    	    } catch (NumberFormatException nfe) {
    	    	System.out.println("You did not enter a valid number:  " + "\""+ num + "\"  is not a number!!");
    	    	return;
    	    } catch (IOException e) {
    	    	System.out.println("IOException: " + e);
    	    	return;
    	    }		
    	    
    	    System.out.println("\nThe two numbers you entered was: " + num + " and " + num2);
    	    System.out.println("Please select an option of what you would like to do with these numbers: ");
    	    
    	    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");
    		System.out.println("5 - Maximum value of two numbers");
    		System.out.println("6 - Minimum value of two numbers");
    		System.out.println("7 - Absolute value of number");
    		System.out.println("8 - Rounded number");
    		System.out.println("9 - Exit");
    		System.out.println("**********************************************");
    		
    		
    		try {
    	    	BufferedReader inStream2 = new BufferedReader (
                                          new InputStreamReader(System.in)
                                      );
    	    	System.out.println("Please enter your option:");
    	    	choice = inStream2.readLine();
    	    	checkNumber    = (int) Integer.parseInt(choice);
    	    	System.out.println("\nYou have entered choice number: " + choice);
    	    } catch (NumberFormatException nfe) {
    	    	System.out.println("You did not enter a valid  choice number:  " + "\""+ choice + "\"  is not on the list!!");
    	    	return;
    	    } catch (IOException e) {
    	    	System.out.println("IOException: " + e);
    	    	return;
    	    }	
    	}
    
    }
    Please have a look through my code and let me know where I have gone wrong.

    Much appreciated.

    Smirre

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Please post the full error message you're getting, or the output it gives, and what it should give. Also... As a first year's Java student (I suppose you're that when you just started studying Java) you already gotta learn bitwise operations? Can't imagine.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    It runs fine. There are no compiler errors just logic errors.
    After I enter my name and surname it asks me to enter a number. I have no idea what this number is or if it's just there to test number input? If I don't enter a number it displays "You did not enter a valid choice number...." but has no option to re-enter anything. You should fix it so it just returns to the Enter a number part instead of having to rerun the program.

    Actually those 2 numbers you enter are used to calculate with. To make it more user friendly you might want to give the calculator options first and then have the user input the numbers.

    You seem to be on the right track. Now you just need to create methods for each calculator option and then output the results. I'm guessing your going to keep track of how long they used the program by subtracting the time they "logged in" from the time they "log out" or do you have some sort of counter?

  4. #4
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    Well here is the output it gives me so far,

    Please enter your name and surname :
    Johnny Bravo

    A warm welcome to you Johnny Bravo ,todays date is : Thursday, 06 November 2008
    and the time is now exactly 06:29 PM.

    Please enter a number and hit <ENTER>: 87

    The number you have entered is: 87

    Please enter another number and hit <ENTER>: 69

    The number you have entered is: 69

    The two numbers you entered was: 87 and 69
    Please select an option of what you would like to do with these numbers:

    *********************************************
    ---------------------------------------------
    Please select an option from the list below:
    ---------------------------------------------
    1 - Add
    2 - Subtract
    3 - Multiply
    4 - Divide
    5 - Maximum value of two numbers
    6 - Minimum value of two numbers
    7 - Absolute value of number
    8 - Rounded number
    9 - Exit
    **********************************************
    Please enter your option:
    1

    You have entered choice number: 1
    There are no error messages when I run the program, but the thing is I don't know what to do further when the user enters an option from the menu for e.g. '1'..

  5. #5
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    xcalmejudasx, it makes alot of sense what you say but I'm not sure how do I go about so that the program returns to
    the "enter a number part" instead of having to rerun the whole program??

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    If you put your error checking and number entry put into seperate methods. Use a boolean value in your checking to say if the numbers are correct or not and call the specified method. If they aren't call the number entry method, if they are, call the "select an option" method and send those numbers in.

    Java Code:
    //Method check
    	public void check(int num, int num2){
    		//Do your error checking in here
    		if(checking passes(pass = true)){
    			optionsMethod();
    		}
    		else(pass = false){
    			inputNumbers();
    		}
    	}

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Where is the "enter the number" part? I searched for that string and didn't find it in your code.

    Sounds like you need a loop with the "enter the number" part at the top of the loop.

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by Norm View Post
    Where is the "enter the number" part? I searched for that string and didn't find it in your code.

    Sounds like you need a loop with the "enter the number" part at the top of the loop.
    Java Code:
    System.out.print("\nPlease enter a number and hit <ENTER>: ");
    	    	num = inStream.readLine();
    	    	checkNumber    = (int) Double.parseDouble(num);
    	    	System.out.println("\nThe number you have entered is: " + num);
    	    	System.out.print("\nPlease enter another number and hit <ENTER>: ");
    	    	num2 = inStream.readLine();
    	    	checkNumber    = (int) Double.parseDouble(num2);
    	    	System.out.println("\nThe number you have entered is: " + num2);
    This is in his first try block.

    I think breaking everything up into smaller methods and putting the try/catch blocks in those would make the program a little easier to follow and give you more control over everything since you can just *shivers* "goto" the section you want.

    (memory)
    ahh the joys of goto. realizing you forgot something and having to renumber hundreds of lines of code and then go and change every single goto state to accommodate the changes. man i love method calls
    (/memory)

Similar Threads

  1. java project
    By gvdn in forum Advanced Java
    Replies: 4
    Last Post: 07-13-2008, 06:45 AM
  2. UML to Java project
    By banie in forum NetBeans
    Replies: 3
    Last Post: 01-28-2008, 10:16 AM
  3. Help Java project.
    By mandrake446 in forum New To Java
    Replies: 1
    Last Post: 11-26-2007, 11:52 PM
  4. Java Project Help
    By Gambit17 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 11:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •