Page 1 of 2 12 LastLast
Results 1 to 20 of 39
  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Writing Switch statement

    Hey guys! I'm writing a program to help patients decide which kind of room they would like to stay in, when they get hospitalized. I have written all of it so far myself and I am curious if it is written correctly (I dont have any errors, but netbeans wont let me run it). Also I'm certain it is unfinished, I am still working on it, I would love some input and/or help on any next steps.

    Heres my 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;
        
        Hospital(int d, char p)
        {
            days = d;
            packageType = p;
        }
        
        boolean isGreaterThanZero()
        {
            return days > 0;
        }
        
        boolean exceed31()
        {
            return days > MAX_DAYS;
        }
        
        String getPackage()
        {
            return message;
        }
        
        double getCharges()
        {
            return charges;
        }
        
        void calculateCharges()
        {
            switch(packageType)
            {
                case 'a': case 'A':
                    message = "Package P";
                    charges = PACKAGE_P;
                    if(days>10) 
                        charges = (charges*days) + telephone + television + (medication*2);
                    break;
                    
                case 'b': case 'B':
                    message = "Package S";
                    charges = PACKAGE_S;
                    if(days>20)
                        charges = (charges*days) + telephone + medication;
                    break;
                    
                case 'c': case 'C':
                    message = "Package W";
                    charges = PACKAGE_W;
                    if(days>31)
                        charges = (charges*days) + (medication/2);
                    break;
            }
        }
    }

    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.text.NumberFormat;
    
    public class TestHospital {
        
        public static void main (String args[])
        {
            Hospital h = new Hospital(200, 'H');
            
            NumberFormat f = NumberFormat.getCurrencyInstance();
            
            display(f);
            
            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 is " + h.getPackage() + "\nYour charge for this month is " + h.getCharges());
                }
            }
            else
                System.out.println("Error: Cannot find patient.");
        }
        static void display(NumberFormat f)
        {
            
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Writing Switch statement

    What exactly do you mean when you say that netbeans won't let you run it? Do you get a compiler error? A runtime error? Something else?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Writing Switch statement

    It just wont let me. The "play" button and everything next to it is greyed out. It will not let me run my program, only this program :(
    Last edited by sithclone3; 07-01-2014 at 10:50 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: Writing Switch statement

    The "play" button and everything next to it is greyed out.
    Is this an IDE setup problem? Is the "everything" part of the IDE?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Writing Switch statement

    I'm not sure, I havn't done anything different from what I normally do in my past programs...

    EDIT: It's the "Build Project" and "Run Project" and "Debug project" stuff thats greyed out
    Last edited by sithclone3; 07-01-2014 at 11:03 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: Writing Switch statement

    Can you use the javac program to compile it to see if there are any compiler errors?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Writing Switch statement

    uuuhhh...I dont know how to use the Javac command....

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: Writing Switch statement

    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Writing Switch statement

    im getting 'javac' is not recognized.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: Writing Switch statement

    Learning how to use javac with source files in packages might take longer than having some one show you how to use your IDE. Do you have someone that know how to use your IDE?

    Get the full path to the javac.exe file and use that. For example here is the command line I use:
    D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint TestCode18.java
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Writing Switch statement

    My professor but he's about as reliant as michael bradley.

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Writing Switch statement

    Why not save the source you have written thus far to a text file. Then delete that project and create a new one. Then paste the source into the new project. You clearly have used NetBeans successfully in the past so you must have done something different that is causing the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Writing Switch statement

    the only thing different is im writing a switch statement..but ill give it a try.

    UPDATE: JIM you've come through yet again! It runs now! :D
    Last edited by sithclone3; 07-02-2014 at 01:43 AM.

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Writing Switch statement

    I also presume you realize you can only run the program which has the static main entry point in it. If you try to run the other class, it won't work (in Eclipse, it doesn't give you the option. I don't know what NetBeans does).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Writing Switch statement

    I got rid of some unnecessary code (NumberFormat mainly) but my output everytime is this:
    Java Code:
    Today is: Jul 1, 2014
    Your package: No such package existts
    Your charge for this month: 0.0
    BUILD SUCCESSFUL (total time: 0 seconds)

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

    Default Re: Writing Switch statement

    NewTest Class:

    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(5, '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("Your package: " + h.getPackage() + "\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;
        
        Hospital(int d, char p)
        {
            days = d;
            packageType = p;
        }
        
        boolean isGreaterThanZero()
        {
            return days > 0;
        }
        
        boolean exceed31()
        {
            return days > MAX_DAYS;
        }
        
        String getPackage()
        {
            return message;
        }
        
        double getCharges()
        {
            return charges;
        }
        
        void calculateCharges()
        {
            switch(packageType)
            {
                case 'a': case 'A':
                    message = "Package P";
                    charges = PACKAGE_P;
                    if(days>10) 
                        charges = charges + days + telephone + television + (medication*2);
                    break;
                    
                case 'b': case 'B':
                    message = "Package S";
                    charges = PACKAGE_S;
                    if(days>20)
                        charges = charges + days + telephone + medication;
                    break;
                    
                case 'c': case 'C':
                    message = "Package W";
                    charges = PACKAGE_W;
                    if(days>31)
                        charges = charges + days + (medication/2);
                    break;
                    
                default:
                    message = "No such package existts";
                    charges = 0;
                    break;
                    
            }
        }
    }

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Writing Switch statement

    One other suggestion which can make you code a tad cleaner. The Character class has some static methods for converting chars to upper or lower case as desired. If your program design allows it, you can store the char as either upper or lower case. That allows you to remove one of the cases for the switch statement (i.e. then you just switch on an upper or lower case version -- your choice).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default Re: Writing Switch statement

    One useful thing while debugging is to display the value of the variable that sent the switch statement to the default: clause.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Writing Switch statement

    Well, your no of days is 200 and the code is H. Since your day test is being ignored or failing, you are calculating charges based on a default (since case 'H' doesn't exist) and thus the message. I would do what Norm said. Print out the fields of the Hospital class after you create the instance and also in some of your related methods.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    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
    Well, your no of days is 200 and the code is H. Since your day test is being ignored or failing, you are calculating charges based on a default (since case 'H' doesn't exist) and thus the message. I would do what Norm said. Print out the fields of the Hospital class after you create the instance and also in some of your related methods.

    Regards,
    Jim

    The new testClass shows me using 5 days and the ward package. I get the same result with any number equal too or under 31...Im not sure why.. Ill look into the character static method, i think i know what you're talking about, i saw my professor use it in class, its something like
    Java Code:
    ignoreCase
    or something right?

Page 1 of 2 12 LastLast

Similar Threads

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