Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
  1. #21
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Writing Switch statement

    Unless your code has changed:
    Java Code:
    Hospital h = new Hospital(5, 'W');
    you are supplying the Hospital class with a 'package' code of 'W'.

    Java Code:
    case 'a': case 'A':
        break;
    case 'b': case 'B':
        break;
    case 'c': case 'C':
        break;
    That's your case statement (excluding default).
    There is no 'W' in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  2. #22
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    Quote Originally Posted by Tolls View Post
    Unless your code has changed:
    Java Code:
    Hospital h = new Hospital(5, 'W');
    you are supplying the Hospital class with a 'package' code of 'W'.
    .
    He changed his code from what he had earlier in the original post.

    Java Code:
    Hospital h = new Hospital(200, 'H');
    I didn't catch the change.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Writing Switch statement

    Switch statement hasn't changed, though.
    'W' or 'H', doesn't matter if the case statement only covers A-C.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Writing Switch statement

    The code should display the incorrect value in the default: clause so the OP would see the wrong value and know why the switch was not doing what he wanted.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    Quote Originally Posted by Tolls View Post
    Switch statement hasn't changed, though.
    'W' or 'H', doesn't matter if the case statement only covers A-C.
    Your correct, except that he questioned his "new" output. Before, since the number of days exceeded 31, he got a different message. That was what I was focusing on.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Writing Switch statement

    Quote Originally Posted by jim829 View Post
    Your correct, except that he questioned his "new" output. Before, since the number of days exceeded 31, he got a different message. That was what I was focusing on.

    Regards,
    Jim
    And his new output is (unless his code has changed since post #16) caused by supplying a 'W', which isn't in the switch.
    As I say, I'm going based on the code as posted...or I'm simply confused.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #27
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    Quote Originally Posted by Tolls View Post
    And his new output is (unless his code has changed since post #16) caused by supplying a 'W', which isn't in the switch.
    As I say, I'm going based on the code as posted...or I'm simply confused.
    :)
    No you are not confused. I was still referring to his original post. So when he told me his code was now working but he was getting different output (post #16), it never dawned on me that he would change the constructor values (post #15). So I assumed it was still 200,H but he changed it to 5,W. So the code is now working as it should but probably not as the OP wants.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #28
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Writing Switch statement

    Quote Originally Posted by jim829 View Post
    No you are not confused. I was still referring to his original post. So when he told me his code was now working but he was getting different output (post #16), it never dawned on me that he would change the constructor values (post #15). So I assumed it was still 200,H but he changed it to 5,W. So the code is now working as it should but probably not as the OP wants.

    Regards,
    Jim
    You're correct, its not working like I want it too, in a way. I noticed i ha it as if(days>10), if(days>20) etc. I changed it to day<10, days<20. This has made everthing work almost as I want it. But roomcharges come out weird I have it set upto multiply roomCharges by #ofdays but I'm confused as to how I get the numbers I do.


    sorry for misspellin, the keyboar im using has those keys you need to press really hard to use. *dells* :/


    TestClass:

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assignment5;
    
    /**
     *
     * @author sithr_000
     */
    
    import java.util.Date;
    import java.text.DateFormat;
    
    
    public class TestHospital {
        
        public static void main (String args[])
        {
            Hospital h = new Hospital(11, 'S');
            
            Date d = new Date(); 
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            System.out.println("Today is: " + df.format(d));
            
            
            
            if(h.isGreaterThanZero())
            {
                if(h.exceed31())
                    System.out.println("This patient has stayed passed the allowed number of days");
                else
                {
                    h.calculateCharges();
                    System.out.println("Your package: " + h.getPackage() + "\n" + "Room Charge: " + h.roomCharge() +
                            "\nTelephone Charge: " + h.telephone + "\nTelevision Charge: " + h.television +
                            "\nMedication Charge: " + h.medication + "\nYour charge for this month: " + h.getCharges());
                }
            }
            else
                System.out.println("Error: Cannot find patient.");
        }
    }
    MainClass
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package assignment5;
    
    /**
     *
     * @author sithr_000
     */
    public class Hospital {
        static final double PACKAGE_P = 550.00,
                            PACKAGE_S = 350.00,
                            PACKAGE_W = 105.00;
        static final double telephone = 4.50;
        static final double television = 7.50;
        static final double medication = 275.00;
        static final int MAX_DAYS = 31;
        
        char packageType;
        String message, name;
        double totalAmount, charges;
        int days;
        double roomCharge;
        
        
        Hospital(int d, char p)
        {
            days = d;
            packageType = p;
        }
        
        double roomCharge()
        {
            return packageType*days;
        }
        
        boolean isGreaterThanZero()
        {
            return days > 0;
        }
        
        boolean exceed31()
        {
            return days > MAX_DAYS;
        }
        
        String getPackage()
        {
            return message;
        }
        
        double getCharges()
        {
            return charges;
        }
        
        void calculateCharges()
        {
            switch(packageType)
            {
                case 'p': case 'P':
                    message = "Package P";
                    charges = PACKAGE_P;
                    if(days<10) 
                        charges = (charges*days) + telephone + television + (medication*2);
                    break;
                    
                case 's': case 'S':
                    message = "Package S";
                    charges = PACKAGE_S;
                    if(days<20)
                        charges = (charges*days) + telephone + medication;
                    break;
                    
                case 'w': case 'W':
                    message = "Package W";
                    charges = PACKAGE_W;
                    if(days<31)
                        charges = (charges*days) + (medication/2);
                    break;
                    
                default:
                    message = "No such package existts";
                    charges = 0;
                    break;
                    
            }
        }
    }
    OUTPUT:

    Java Code:
    Today is: Jul 2, 2014
    Your package: Package S
    Room Charge: 1265.0
    Telephone Charge: 4.5
    Television Charge: 7.5
    Medication Charge: 275.0
    Your charge for this month: 4129.5

    what I should be getting for roomCharges is $3,850, but instead I get 1265...from 350*11 im confused haha! My medication is not being calculated right either...but I ave it set in my constructor and my charges for the month are off because of this miscalculation with the roomcharges.
    Last edited by sithclone3; 07-02-2014 at 10:34 PM.

  9. #29
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    The problem is when you call roomCharge(), you are calculating packageType * days. packageType is a char and is allowed to participate in arithmetic operations. So the character 'S' has a value of 83 so your room charges should be 83*11 or 913. However, lower case 's' has a value 115 so 115 *11 = 1265 which is what you are showing. So either somewhere you converted your 'S' to lower case, or your posted output is not from the posted code. Or I don't know what I am talking about.

    Regards,
    Jim
    Last edited by jim829; 07-03-2014 at 12:33 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #30
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Writing Switch statement

    ok, I checked and the upper/lower case version give the same number, its just strange that its not calculating like it should, honestly I dont know what its multiplying by 0.o. Im just gunna mess around see if i can fix it.

  11. #31
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    Have you added a bunch of print statements to the code at various places to see if the variables are what you expect them to be at certain times during execution of the program? The print statement is the easiest and arguably the most popular debugging tool there is.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #32
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    I just ran your last posted code. When I use 's' the room charges are 1265. When I use 'S' the room charges are 913. Are you certain you are using the code you posted?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #33
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Writing Switch statement

    Yeah When I did it and nothing changed, I did it again and it did. weird, it gave me like a $400 difference...I find that really strange..Anyone know what could cause this? 0.0

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Writing Switch statement

    .Anyone know what could cause this
    post the current code and the command prompt window's contents that shows what the code output when executed.
    If you don't understand my response, don't ignore it, ask a question.

  15. #35
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Writing Switch statement

    Current MainClass:

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package assignment5;
    
    /**
     *
     * @author sithr_000
     */
    public class Hospital {
        static final double PACKAGE_P = 550.00,
                            PACKAGE_S = 350.00,
                            PACKAGE_W = 105.00;
        static final double telephone = 4.50;
        static final double television = 7.50;
        static final double medication = 275.00;
        static final int MAX_DAYS = 31;
         
        char packageType;
        String message, name;
        double totalAmount, charges;
        int days;
        double roomCharge;
         
         
        Hospital(int d, char p)
        {
            days = d;
            packageType = p;
        }
        
        double roomCharge()
        {
            return packageType*days;
        }
         
        boolean isGreaterThanZero()
        {
            return days > 0;
        }
         
        boolean exceed31()
        {
            return days > MAX_DAYS;
        }
         
        String getPackage()
        {
            return message;
        }
         
        double getCharges()
        {
            return charges;
        }
         
        void calculateCharges()
        {
            switch(packageType)
            {
                case 'p': case 'P':
                    message = "Package P";
                    charges = PACKAGE_P;
                    if(days<10) 
                        charges = charges + roomCharge() + telephone + television + (medication*2);
                    break;
                     
                case 's': case 'S':
                    message = "Package S";
                    charges = PACKAGE_S;
                    if(days<20)
                        charges = charges + roomCharge() + telephone + medication;
                    break;
                     
                case 'w': case 'W':
                    message = "Package W";
                    charges = PACKAGE_W;
                    if(days<31)
                        charges = charges + roomCharge() + (medication/2);
                    break;
                     
                default:
                    message = "No such package existts";
                    charges = 0;
                    break;
                     
            }
        }
    }

    Current TestClass
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assignment5;
     
    /**
     *
     * @author sithr_000
     */
     
    import java.util.Date;
    import java.text.DateFormat;
     
     
    public class testHospital {
         
        public static void main (String args[])
        {
            Hospital h = new Hospital(11, 'W');
             
            Date d = new Date(); 
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            System.out.println("Today is: " + df.format(d));
             
            if(h.isGreaterThanZero())
            {
                if(h.exceed31())
                    System.out.println("This patient has stayed passed the allowed number of days");
                else 
                {
                    h.calculateCharges();
                    System.out.println("Patient name: " + "\nYour package: " + h.getPackage() + "\n" + "Room Charge: " + h.roomCharge() +
                            "\nTelephone Charge: " + h.telephone + "\nTelevision Charge: " + h.television +
                            "\nMedication Charge: " + h.medication + "\nYour charge for this month: " + h.getCharges());
                }
            }
            else 
                System.out.println("Error: Cannot find patient.");
        }
    }

    OUTPUT from current input:

    Java Code:
    run:
    Today is: Jul 2, 2014
    Patient name: 
    Your package: Package W
    Room Charge: 957.0
    Telephone Charge: 4.5
    Television Charge: 7.5
    Medication Charge: 275.0
    Your charge for this month: 1199.5
    BUILD SUCCESSFUL (total time: 0 seconds)

    EDIT: I just want to say this is the first project I have started myself without any help or any of the code written to get me started. I really want to get this working :(

  16. #36
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    First, I think you pasted incorrectly. There is no h.room. I will assume that is h.roomCharges(). I will also repeat myself. You are multiplying the letter W by the number of days. The letter W has a value of 87. 11 x 87 = 957. Your room charge should be based on the room plan, not the value the lets the program select the room plan. This is based on your previous message of what the answer should be. In this case it should be 11 x PACKAGE_W or 11 x 105.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #37
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Writing Switch statement

    Quote Originally Posted by jim829 View Post
    First, I think you pasted incorrectly. There is no h.room. I will assume that is h.roomCharges(). I will also repeat myself. You are multiplying the letter W by the number of days. The letter W has a value of 87. 11 x 87 = 957. Your room charge should be based on the room plan, not the value the lets the program select the room plan. This is based on your previous message of what the answer should be. In this case it should be 11 x PACKAGE_W or 11 x 105.

    Regards,
    Jim
    Yes sir 0.o. So I changed roomCharges to return charges * days This works really well for Private, but nothing changes with medication and it still is funky with Ward and Sem-private
    Last edited by sithclone3; 07-03-2014 at 08:22 PM.

  18. #38
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Writing Switch statement

    That's because you are computing you charges at different places. In your switch statements you have medication/2 and medication * 2 depending on the rate. So you store all this in charges but you don't update the actual medication value. So when you print it out you just use the default medication value of 275.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #39
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Writing Switch statement

    Ok I fixed everything! :3 its working like it should and how I want it!

    NewMainClass:
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package assignment5;
    
    /**
     *
     * @author sithr_000
     */
    public class Hospital {
        static final double PACKAGE_P = 550.00,
                            PACKAGE_S = 350.00,
                            PACKAGE_W = 105.00;
    
        static final int MAX_DAYS = 31;
       
        double telephone = 4.50;
        double television = 7.50;
        double medication = 275.00;
        char packageType;
        String message, name;
        double totalAmount, charges;
        int days;
        double roomCharge;
        double updatedMedication;
         
        Hospital(int d, char p)
        {
            days = d;
            packageType = p;
        }
        
        double roomCharge()
        {
            return charges*days;
        }
         
        boolean isGreaterThanZero()
        {
            return days > 0;
        }
         
        boolean exceed31()
        {
            return days > MAX_DAYS;
        }
         
        String getPackage()
        {
            return message;
        }
         
        double getCharges()
        {
            return charges;
        }
         
        void calculateCharges()
        {
            switch(packageType)
            {
                case 'p': case 'P':
                    message = "Private";
                    charges = PACKAGE_P;
                    if(days<10) 
                        updatedMedication = medication*2;
                        totalAmount = totalAmount + roomCharge() + telephone + television + updatedMedication;
                    break;
                     
                case 's': case 'S':
                    message = "Semi-Private";
                    charges = PACKAGE_S;
                    if(days<20)
                        television = 0;
                        updatedMedication = medication;
                        totalAmount = totalAmount + roomCharge() + telephone + medication;
                    break;
                     
                case 'w': case 'W':
                    message = "Ward";
                    charges = PACKAGE_W;
                    if(days<31)
                        updatedMedication = medication/2;
                        telephone = 0;
                        television = 0;
                        totalAmount = totalAmount + roomCharge() + updatedMedication;
                    break;
                     
                default:
                    message = "No such package existts";
                    charges = 0;
                    break;
                     
            }    
        }
    }
    NewTestClass:
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assignment5;
     
    /**
     *
     * @author sithr_000
     */
     
    import java.util.Date;
    import java.text.DateFormat;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
     
    public class testHospital {
         
        public static void main (String args[])
        {
            Hospital h = new Hospital(5, 'P');
            
            String name = GetData.getWord("Enter full name");
             
            Date d = new Date(); 
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            System.out.println("Today is: " + df.format(d));
             
            if(h.isGreaterThanZero())
            {
                if(h.exceed31())
                    System.out.println("This patient has stayed passed the allowed number of days");
                else 
                {
                    h.calculateCharges();
                    System.out.println("Patient name: " + name + "\nYour room type: " + h.getPackage() + "\n" + "Room Charge: " + h.roomCharge() +
                            "\nTelephone Charge: " + h.telephone + "\nTelevision Charge: " + h.television +
                            "\nMedication Charge: " + h.updatedMedication + "\nYour charge for this month: " + h.totalAmount);
                }          
            }
            else 
                System.out.println("Error: Cannot find patient.");
        }
    }
    GetDataClass:
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assignment5;
    
    /**
     *
     * @author sithr_000
     */
    import javax.swing.JOptionPane;
    
    class GetData {
    
        static double getDouble(String s) {
            return Double.parseDouble(getWord(s));
        }
    
        static char getChar(String s) {
            String str = getWord(s);
            return str.charAt(0);
        }
    
        static int getInt(String s) {
            return Integer.parseInt(getWord(s));
        }
    
        static String getWord(String s) {
            return JOptionPane.showInputDialog(s);
        }
    }
    Last edited by sithclone3; 07-04-2014 at 02:58 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 09:12 PM
  2. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  3. switch statement
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 10:54 AM
  4. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 03:22 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 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
  •