Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • ASDss: where do u download source and clients now
      August 18, 2017, 10:39:31 PM
    • ASDss: yo
      August 18, 2017, 10:39:20 PM
    • dan v jad: click me 4 da fun ;)[link]
      August 18, 2017, 04:39:58 PM
    • stCky: Palidinho is your OpenGL (was it OpenGL?) stuff open source anywhere?
      August 16, 2017, 09:07:22 PM
    • Travas:BUILD THE WALL
      August 15, 2017, 09:28:49 PM
    • Travas: i have ass cancer
      August 15, 2017, 09:23:29 PM
    • stCky: what are the fudge are you tryna ask?
      August 15, 2017, 08:21:35 PM
    • bader: what are the rsps community alive ?
      August 15, 2017, 05:46:16 PM
    • bader: yo guys
      August 15, 2017, 05:46:08 PM
    • Spacehost:[link] Updated our thread :)
      August 15, 2017, 09:40:34 AM
    • Adaro: The client is in Download section at Homepage
      August 15, 2017, 01:09:20 AM
    • FaTe_Of_GoDs: where do i get the client?????????????
      August 14, 2017, 05:23:14 PM
    • stCky: can anyone help me? I cant login to the shoutbox
      August 13, 2017, 05:45:15 PM
    • drubrkletern: appeal denied
      August 13, 2017, 02:35:27 PM
    • King_Trout:[link]
      August 13, 2017, 11:17:12 AM
    • Cole1497: no sorry
      August 13, 2017, 10:27:14 AM
    • ayz: yo can anyone explain something to me
      August 13, 2017, 08:08:51 AM
    • coolking12: Hi
      August 13, 2017, 04:16:06 AM
    • stCky: n+1
      August 11, 2017, 06:09:24 PM
    • PalidinoDH: How many more pages are going to show errors before this dude gets on and fixes shit
      August 11, 2017, 04:57:00 PM

    Author Topic: Need some help with Java  (Read 3568 times)

    0 Members and 1 Guest are viewing this topic.

    Offlinepk4pk

    • Member
    • ****
    • Posts: 1,874
    • Thanks: +0/-0
      • View Profile
    Need some help with Java
    « on: November 06, 2014, 05:31:53 PM »
    So here is the code:

    Code: [Select]
             Scanner console = new Scanner(System.in);
             System.out.println("How much will John be spending?");
             double amount = console.nextDouble();
             System.out.println();
             int numBills1 = (int) (amount/20.0);
             if (numBills1 * 20.0 < amount) {
                numBills1++;
            }
            System.out.println("How much will Jane be spending?");
            amount = console.nextDouble();
            System.out.println();
            int numBills2 = (int) (amount / 20.0);
            if (numBills2 * 20.0 < amount) {
                numBills2++;
            }
            System.out.println("John needs " + numBills1 + " bills");
            System.out.println("Jane needs " + numBills2 + " bills");

    Can someone help me restructure this so it isn't redundant? I believe I need to add a method requiring at least one or more parameters. Any help is appreciated.

    Offlinet4

    • Member
    • ****
    • *
    • *
    • *
    • Posts: 6,798
    • Thanks: +0/-0
      • View Profile
    Re: Need some help with Java
    « Reply #1 on: November 06, 2014, 06:06:35 PM »
    Code: Java
    1. String[] names =newString[]{"John", "Jane"};
    2. for(int i =0; i < names.length;++i){
    3.          Scanner console =new Scanner(System.in);
    4.          System.out.println("How much will "+ names[i]+" be spending?");
    5.          double amount = console.nextDouble();
    6.          System.out.println();
    7.          int numBills =(int)(amount/20.0);
    8.          if(numBills *20.0< amount){
    9.             numBills++;
    10.         }
    11.         System.out.println(names[i]+" needs "+ numBills +" bills");
    12. }
    13.  

    Offlinepk4pk

    • Member
    • ****
    • Posts: 1,874
    • Thanks: +0/-0
      • View Profile
    Re: Need some help with Java
    « Reply #2 on: November 06, 2014, 06:55:07 PM »
    Code: Java
    1. String[] names =newString[]{"John", "Jane"};
    2. for(int i =0; i < names.length;++i){
    3.          Scanner console =new Scanner(System.in);
    4.          System.out.println("How much will "+ names[i]+" be spending?");
    5.          double amount = console.nextDouble();
    6.          System.out.println();
    7.          int numBills =(int)(amount/20.0);
    8.          if(numBills *20.0< amount){
    9.             numBills++;
    10.         }
    11.         System.out.println(names[i]+" needs "+ numBills +" bills");
    12. }
    13.  

    Awesome thanks so much.

    Also need some help with this:

    Quote
    Write a piece of code that reads a shorthand text description of a color and prints the longer equivalent. Acceptable color names are B for Blue, G for Green, and R for Red. If the user types something other than B,G, OR R, the program should print an error message. Make your program case-insensitive so that the user can type an uppercase or lowercase letter. Here are some example executions:

    What color do you want? B
    You have chosen Blue.

    What color do you want? R
    You have chosen Green.

    What color do you want? Bork
    Unknown color: Bork

    and

    Quote
    Write a piece of code that reads a shorthand text description of a playing card and prints the longhand equivalent. The shorthand description is the card's rank (2 through 10, J, Q, K, or A) followed by its suit (C, D, H, or S). You should expand the shorthand into the form "<Rank> of <Suit>". You may assume that the user types valid input. Here are two sample executions:

    Enter a card: 9 S
    Nine of Spades

    Enter a card: K C
    King of Clubs


    OfflineRyley

    • Member
    • ****
    • *
    • Posts: 7,315
    • Thanks: +0/-0
      • View Profile
    Re: Need some help with Java
    « Reply #3 on: November 06, 2014, 07:29:04 PM »
    Code: Java
    1. String[] names =newString[]{"John", "Jane"};
    2. for(int i =0; i < names.length;++i){
    3.          Scanner console =new Scanner(System.in);
    4.          System.out.println("How much will "+ names[i]+" be spending?");
    5.          double amount = console.nextDouble();
    6.          System.out.println();
    7.          int numBills =(int)(amount/20.0);
    8.          if(numBills *20.0< amount){
    9.             numBills++;
    10.         }
    11.         System.out.println(names[i]+" needs "+ numBills +" bills");
    12. }
    13.  

    Awesome thanks so much.

    Also need some help with this:

    Quote
    Write a piece of code that reads a shorthand text description of a color and prints the longer equivalent. Acceptable color names are B for Blue, G for Green, and R for Red. If the user types something other than B,G, OR R, the program should print an error message. Make your program case-insensitive so that the user can type an uppercase or lowercase letter. Here are some example executions:

    What color do you want? B
    You have chosen Blue.

    What color do you want? R
    You have chosen Green.

    What color do you want? Bork
    Unknown color: Bork

    and

    Quote
    Write a piece of code that reads a shorthand text description of a playing card and prints the longhand equivalent. The shorthand description is the card's rank (2 through 10, J, Q, K, or A) followed by its suit (C, D, H, or S). You should expand the shorthand into the form "<Rank> of <Suit>". You may assume that the user types valid input. Here are two sample executions:

    Enter a card: 9 S
    Nine of Spades

    Enter a card: K C
    King of Clubs

    Have you tried yourself?

    Offlinepk4pk

    • Member
    • ****
    • Posts: 1,874
    • Thanks: +0/-0
      • View Profile
    Re: Need some help with Java
    « Reply #4 on: November 06, 2014, 08:46:25 PM »
    This is what I've got for the second one but I'm not sure if it's the best way to do it:

    Code: Java
    1.         publicstaticvoid main(String[] args){
    2.                 Scanner console =new Scanner(System.in);
    3.                 color(console);
    4.         }
    5.        
    6.         publicstaticvoid color(Scanner console){
    7.                 System.out.println("What color would you like to select?");
    8.                 String colorCode = console.next();
    9.                
    10.                 if(colorCode.equals("R")|| colorCode.equals("r"))
    11.                 {
    12.                         System.out.println("You selected red.");
    13.                 }
    14.                 elseif(colorCode.equals("G")|| colorCode.equals("g")){
    15.                         System.out.println("You selected green.");
    16.                 }
    17.                 elseif(colorCode.equals("B")|| colorCode.equals("b")){
    18.                         System.out.println("You selected blue.");
    19.                 }
    20.                 else{
    21.                         System.out.println("Unknown color: "+ colorCode);
    22.                         color(console);
    23.                 }
    24.         }
    25.  
    26. }
    27.  
    I'm still working on the third one which is quite difficult. I've only been working with Java for just over a month so I'm not the best yet.

    Offlinet4

    • Member
    • ****
    • *
    • *
    • *
    • Posts: 6,798
    • Thanks: +0/-0
      • View Profile
    Re: Need some help with Java
    « Reply #5 on: November 06, 2014, 09:40:07 PM »
    Write a piece of code that reads a shorthand text description of a color and prints the longer equivalent. Acceptable color names are B for Blue, G for Green, and R for Red. If the user types something other than B,G, OR R, the program should print an error message. Make your program case-insensitive so that the user can type an uppercase or lowercase letter. Here are some example executions:

    What color do you want? B
    You have chosen Blue.

    What color do you want? R
    You have chosen Green.

    What color do you want? Bork
    Unknown color: Bork
    Code: Java
    1. String[] cstrs =newString[26];
    2. cstrs[(int)'R'-(int)'A']="Red";
    3. cstrs[(int)'G'-(int)'A']="Green";
    4. cstrs[(int)'B'-(int)'A']="Blue";
    5.  
    6. void printColor(String s){
    7.   if(s.length()>1|| cstrs[(int) s.toUpperCase().charAt(0)-(int)'A']==null){System.err.println("Unknown color: "+ s);return;}
    8.   System.out.println("You have chosen "+ cstrs[(int) s.toUpperCase().charAt(0)-(int)'A']+".");
    9. }
    10.  


    Write a piece of code that reads a shorthand text description of a playing card and prints the longhand equivalent. The shorthand description is the card's rank (2 through 10, J, Q, K, or A) followed by its suit (C, D, H, or S). You should expand the shorthand into the form "<Rank> of <Suit>". You may assume that the user types valid input. Here are two sample executions:

    Enter a card: 9 S
    Nine of Spades

    Enter a card: K C
    King of Clubs
    I won't do this one for you, but I suggest having a Card class with a Rank and Suit enum. You can override the toString() method in the Rank and Suit enums to produce the full name (ex: "Nine" for Rank and "Spades" for Suit). In both enums you can have a method called fromString which takes a String, which is supposed to be a single character (ex: "9" for Rank and "S" for spades) and take the first character in the String and switch on its value returning the correct enum type. If you need help with enums, check out http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
    « Last Edit: November 06, 2014, 09:44:49 PM by t4 »

    Offlinepk4pk

    • Member
    • ****
    • Posts: 1,874
    • Thanks: +0/-0
      • View Profile
    Re: Need some help with Java
    « Reply #6 on: November 06, 2014, 11:22:40 PM »
    Write a piece of code that reads a shorthand text description of a color and prints the longer equivalent. Acceptable color names are B for Blue, G for Green, and R for Red. If the user types something other than B,G, OR R, the program should print an error message. Make your program case-insensitive so that the user can type an uppercase or lowercase letter. Here are some example executions:

    What color do you want? B
    You have chosen Blue.

    What color do you want? R
    You have chosen Green.

    What color do you want? Bork
    Unknown color: Bork
    Code: Java
    1. String[] cstrs =newString[26];
    2. cstrs[(int)'R'-(int)'A']="Red";
    3. cstrs[(int)'G'-(int)'A']="Green";
    4. cstrs[(int)'B'-(int)'A']="Blue";
    5.  
    6. void printColor(String s){
    7.   if(s.length()>1|| cstrs[(int) s.toUpperCase().charAt(0)-(int)'A']==null){System.err.println("Unknown color: "+ s);return;}
    8.   System.out.println("You have chosen "+ cstrs[(int) s.toUpperCase().charAt(0)-(int)'A']+".");
    9. }
    10.  


    Write a piece of code that reads a shorthand text description of a playing card and prints the longhand equivalent. The shorthand description is the card's rank (2 through 10, J, Q, K, or A) followed by its suit (C, D, H, or S). You should expand the shorthand into the form "<Rank> of <Suit>". You may assume that the user types valid input. Here are two sample executions:

    Enter a card: 9 S
    Nine of Spades

    Enter a card: K C
    King of Clubs
    I won't do this one for you, but I suggest having a Card class with a Rank and Suit enum. You can override the toString() method in the Rank and Suit enums to produce the full name (ex: "Nine" for Rank and "Spades" for Suit). In both enums you can have a method called fromString which takes a String, which is supposed to be a single character (ex: "9" for Rank and "S" for spades) and take the first character in the String and switch on its value returning the correct enum type. If you need help with enums, check out http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
    Wow that looks a lot simpler than the way I did it but we haven't gone that far in the class and my professor is expecting us to handle it using arrays. This is how I did it:

    Code: Java
    1.         publicstaticvoid main(String[] args){
    2.                 Scanner console =new Scanner(System.in);
    3.                 Task3(console);
    4.         }
    5.  
    6.         publicstaticvoid Task3(Scanner console){
    7.                 String[] suits ={"Spades", "Hearts", "Diamonds", "Clubs"};
    8.                 String[] ranks ={"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    9.                 String[] cardName ={"S", "H", "D", "C"};
    10.                 String[] cardNums ={"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    11.                 String cardRankVar =" ";
    12.                 String cardSuitVar =" ";
    13.                
    14.                 System.out.println("Select a card: ");
    15.                 String cardRank = console.next();
    16.                 String cardSuit = console.next();
    17.                
    18.                 for(int i =0; i < ranks.length; i++)
    19.                 {
    20.                         if(cardRank.equals(ranks[i]))
    21.                         {
    22.                                 cardRankVar = cardNums[i];
    23.                         }
    24.                 }
    25.                 for(int j =0; j < cardName.length; j++)
    26.                 {
    27.                         if(cardSuit.equals(cardName[j]))
    28.                         {
    29.                                 cardSuitVar = suits[j];
    30.                         }      
    31.                 }
    32.                
    33.                 System.out.print(cardRankVar +" of "+ cardSuitVar);
    34.         }
    35. }
    36.  

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal