Results 1 to 10 of 10

Thread: Elements second Java assignment!

  1. #1
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default Elements second Java assignment!

    First off thanks for the help last time guys! I will post my assignment later tonight once I finish it! But I do have one question about something my teach asked...


    "Two variables*, num and cost have been declared* and given* values*: num is an integer* and cost is a double*. Write a single statement* that outputs* num and cost to standard output*. Print both values* (num first, then cost), separated by a space on a single line that is terminated with a newline character*. Do not output* any thing else."


    No idea what he is really asking for there....



    Code:
    import java.util.Scanner;
    
    /*Andrew Varnick 
      9/7/2013 
      CSCI 1015 
      Programming Assignment 2
     The purpose of this assignment/program is to calculate the number of cents in a specific number of dimes and nickels
    */
    
    public class VarnickPass2
    {
       public static void main(String[] args)
       {
          Scanner keyboard = new Scanner(System.in);
          String name, input1, input2;
          int nickle, dime, total;
          
           
          //Get the user's name
          name =
             keyboard.nextLine();
             
         //Get the number of nickles and dimes
             
          input = keyboard.nextLine();
          input2 = keyboard.nextLine();
         
          
          //Convert the input to Int
          nickle = Integer.parseInt(input);
          dime = Integer.parseInt(input2);
          
          //Gets total
          System.out.println("Total is:" + ((nickle * 5) + (dime * 10)));
         
      
          
          System.exit(0); 
       }
    }
    Assignment #2 !!!!!

    Purpose

    To learn how to create the main java method, explore declaring a variables, using
    arithmetic operators, assignment statements, reading from the keyboard using the
    Scanner class and outputting results using
    System.out class, and adding comments.


    Exercise

    Write a program that will calculate the number of cents in a specific number of dimes
    and nickels. The program will perform the following tasks:
    •Ask the user to enter the number of dimes.
    •Ask the user to enter the number of nickels.
    •Calculate the total number of cents.
    •Display to the monitor the number of cents.
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  2. #2
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    I think you should attempt the exercise first. but

    Code:
      System.out.println(num + " " + cost);

  3. #3
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Code:
        public static void main(String[] args) throws IOException {
            BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter the number of dimes: ");
            String dimes = Br.readLine();
            System.out.print("Enter the number of nickels: ");
            String nickels = Br.readLine();
            int d = 0;
            int n = 0;
            try{
                d = Integer.parseInt(dimes);
                n = Integer.parseInt(nickels);
            }catch(NumberFormatException nfe){
                System.err.println("Enter only numbers!");
            }
            System.out.println("Total value: " + (d * 10 + n * 5) + " cents");            
        }
    Last edited by riwu; 09-09-2013 at 12:18 AM.

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    ..Code..


    This is incorrect.

    Purpose of his exercise:
    Reading from the keyboard using the java.util.Scanner class and outputting results..
    Thus you shouldn't be using a buffered-reader and a try catch for evaluating invalid input.

    Also, you might want to give OP a chance to do the assignment rather than him not an showing any attempt at doing it. He will never learn this way. Other than that, nice code.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Well in the beginning it's best to learn by examples...(unless OP requested to give only hints and not solutions)
    If you have to use the Scanner class:
    Code:
        public static void main(String[] args) {
            Scanner Sc = new Scanner(System.in);
            int d = 0;
            int n = 0;
            System.out.print("Enter the number of dimes: ");
            try {
                d = Sc.nextInt();
            }catch (java.util.InputMismatchException ime) {
                System.err.println("Enter only numbers!");
                System.exit(0);
            }
            System.out.print("Enter the number of nickels: ");
            try {
                n = Sc.nextInt();
            }catch (java.util.InputMismatchException ime) {
                System.err.println("Enter only numbers!");
                System.exit(0);
            }
            System.out.println("Total value: " + (d * 10 + n * 5) + " cents");          
        }

  6. #6
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default

    Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    /*Andrew Varnick 
      9/7/2013 
      CSCI 1015 
      Programming Assignment 2
     The purpose of this assignment/program is to calculate the number of cents in a specific number of dimes and nickels
    */
    
    public class VarnickPass2
    {
       public static void main(String[] args)
       {
          Scanner Sc = new Scanner(System.in);
          String name;
          String input, input2; 
          int nickle, dime, total;
          
           
          //Get the user's name
          name =
             JOptionPane.showInputDialog("Enter your name.");
             
         //Get the number of nickles and dimes
             
          input = JOptionPane.showInputDialog("How many nickles do you have?");
          input2 = JOptionPane.showInputDialog("How many dimes do you have");
         
          
          //Convert the input to Int
          nickle = Integer.parseInt(input);
          dime = Integer.parseInt(input2);
          
          //Gets total
          System.out.println("Total is" + nickle * 5 + dime * 10);
          
      
          
          System.exit(0); 
       }
    }
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  7. #7
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Element17 View Post
    Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    /*Andrew Varnick 
      9/7/2013 
      CSCI 1015 
      Programming Assignment 2
     The purpose of this assignment/program is to calculate the number of cents in a specific number of dimes and nickels
    */
    
    public class VarnickPass2
    {
       public static void main(String[] args)
       {
          Scanner Sc = new Scanner(System.in);
          String name;
          String input, input2; 
          int nickle, dime, total;
          
           
          //Get the user's name
          name =
             JOptionPane.showInputDialog("Enter your name.");
             
         //Get the number of nickles and dimes
             
          input = JOptionPane.showInputDialog("How many nickles do you have?");
          input2 = JOptionPane.showInputDialog("How many dimes do you have");
         
          
          //Convert the input to Int
          nickle = Integer.parseInt(input);
          dime = Integer.parseInt(input2);
          
          //Gets total
          System.out.println("Total is" + nickle * 5 + dime * 10);
          
      
          
          System.exit(0); 
       }
    }
    hmm you instantiated the Scanner class, but ended up using swing GUI for input :S
    Also without putting parenthesis for nickle * 5 + dime * 10, it will concatenate the string rather than the integer sum.

  8. #8
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    I know when i was in Java 1 my prof hated when people did any sort of math in the system.out. line, not sure if yours is the same.
    Last edited by [XoL]; 09-10-2013 at 03:54 PM.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  9. #9
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default

    Quote Originally Posted by [XoL] View Post
    I know when i was in Java 1 my prof hated when people did any sort of math in the system.out. line, not sure if yours is the same.
    He hasn't said anything about it, but how else could I print the results?


    Also to @riwu should I take out the GUI? and should the last line to calculate be something like this

    Code:
           System.out.println("Total is" + ((nickle * 5) + (dime * 10)));
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  10. #10
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Element17 View Post
    He hasn't said anything about it, but how else could I print the results?


    Also to @riwu should I take out the GUI? and should the last line to calculate be something like this

    Code:
           System.out.println("Total is" + ((nickle * 5) + (dime * 10)));
    Xol's prof wants the math to be done in advance, like
    Int value = nickle * 5 + dime * 10;

    You should add parenthesis to the whole math block as seen in my prev 2 code:
    Code:
    System.out.println("Total value: " + (d * 10 + n * 5) + " cents");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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