Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • hexedss1: Anyone would like to help on client side got a variable declartion Error
      Today at 09:06:30 PM
    • SOUPSOUP: just get a vpn
      August 13, 2018, 06:44:28 PM
    • jme: _^_ will answer all of your javascript questions
      August 12, 2018, 07:11:00 PM
    • doomsale: and where is boxman?
      August 10, 2018, 03:16:59 AM
    • doomsale: Where can i play the Moparscape MMORPG with the Metallica instrumentals?
      August 10, 2018, 03:16:47 AM
    • doomsale: no longer clicking on doors and dummies at east bank
      August 10, 2018, 03:16:07 AM
    • doomsale: were all grown up now huh?
      August 10, 2018, 03:15:58 AM
    • _^_: hey everyone look at my graphs [link]
      August 06, 2018, 01:52:16 PM
    • braedyn: Someone pl0x help me out with PI Lottery!!
      August 03, 2018, 02:12:25 AM
    • Travas: p3n1s
      July 26, 2018, 09:46:33 PM
    • Krackkid: You up :eggplant:
      July 23, 2018, 06:18:17 PM
    • DeathsChaos9: no
      July 23, 2018, 03:53:31 PM
    • Emulous: Come check out Emulous: [link]
      July 22, 2018, 03:37:19 PM
    • Emulous:[link]
      July 22, 2018, 03:37:00 PM
    • Travas: ur mom
      July 21, 2018, 06:04:50 PM
    • YNRDAB: Watcha big ballers up too?
      July 20, 2018, 11:01:06 AM
    • dark250: i need help for coding client
      July 19, 2018, 08:27:37 AM
    • Travas: yes
      July 14, 2018, 02:19:44 PM
    • drubrkletern: lmao racist and sexist much?
      July 14, 2018, 01:11:52 PM
    • Cole1497: yeah i got you lil boat give me a second
      July 13, 2018, 03:19:35 AM

    Author Topic: Need some help with Java  (Read 4309 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.
    Runescape Gambling

    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.  
    Runescape Gambling

    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
    Runescape Gambling

    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