Solution: Hospital stay

From the course: Learning Java 8

  • Course details

    Java is one of the top-five programming languages, and is used for websites, embedded controllers, and Android app development. This is an introduction to get you started programming with Java. Peggy Fisher introduces the basics: data types, strings, arrays, expressions, loops, and functions. She'll help you control the flow and logic of your code, and create classes using the principles of object-oriented design. Then go a bit beyond the basics and learn advanced techniques for working with arrays, manipulating files, and building graphical user interfaces (GUIs) that respond to user input. This three-hour capsule course is perfect for developers who need to get up to speed with Java fast, as well as for beginning programmers who want their first taste of this popular language.

    Instructor

    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with Lynda.com content

      • Peggy Fisher is a programmer and content manager at LinkedIn Learning.

        Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - I hope you had fun with this last challenge. The challenge was meant to get you started with adding decision-making logic, loops, and optionally, methods. Let's take a look at my solution. Remember that your solution might be very different. There are a few lines of code that will be new, but I think it will help you understand how you can expand on the information I've already provided. Let's walk through the code. In the main method, I start by declaring variables that I'll need for both an in-hospital and outpatient stay. I have medication charges, lab service charges, hospital charges, and then total cost. I added a string variable called "response." This will allow me to ask the user if they have another patient. If they do, I'll run through the loop again. Otherwise, I'll exit. In my solution, I use a do-while loop. I say, "Do." The first thing I do is check to see if it's an overnight stay. I created a method for this, but you wouldn't have to. Let's take a look at the method real quick. It returns a Boolean variable and it does not take any parameters. It simply asks the user, "Is this an overnight stay?" And I kinda give the user a hint as to what I'm looking for, a Y or an N. I read in their response. If their response equals a Y, I wanna make sure that I don't worry about whether they enter a capital Y or a lower case Y, so I have equalsIgnoreCase. If they did say Y, I return true, otherwise, I return false. So in my decision-logic, on line 29, it says, "if(checkForOvernight() == true)." Don't forget, two equals signs are important because if you only put one, it would always set it to true. Two equals signs compare. One, assigns. If it is an overnight stay, I ask the user to enter the cost of the hospital stay. I read that into my hospital charges. Otherwise, I wanna reset the hospital charges to 0. Remember, this loop is going to allow us to calculate charges for more than one patient. Next, I prompt the user to enter the information about the medication charges and the lab service cost. Now I have all the information I need to calculate the total cost. Total cost is equal to hospital charges, which is zero if there was not an overnight stay, medication charges and lab service charges. I call a method, printTotal, and I give it the total cost so it can print out the total cost for this patient. Now I need to prompt the user to see if they wanna enter information about another patient. Again, I'm looking for a Y or an N. I read in the response, now I down to the While part of my do-while. While the response is equal to yes then I'm gonna go back up top. As soon as this is false, it'll stop the program, and since this is the last line of the program, my program will end. I do wanna point out, do you see the While condition has a semicolon at the end? The only time you'll have that semicolon is when it's a do-while statement. Okay, if you didn't get a chance to try this first, go back and try again. It doesn't have to be exactly like this solution. Create your own! Before we end, let's go ahead and run the program so you can see how it works. I'll click on the green arrow. The first thing is asking us if it's an overnight stay. Let's start by saying yes. Now it wants to know the cost of the hospital stay. Let's put in 1200 dollars. Medication charges, 400. And lab services, 350. The total cost for this patient is $1950. Now you can see I don't have to start the program over again, because I am inside my do-while loop. I'm being prompted to ask if there's another patient. Let's say yes. This time, let's say, "No," to overnight stay. We'll enter our medication charges and our lab service, and that's all we have. So our total should be 1250, and it is. Okay, that's it for this program. I hope you enjoyed this challenge.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.

Contents