This course explores the fundamental concepts and syntax associated with Control Flow and showcases these with some real-life projects.
- Learn about the three categories of control statements: sequential, selection, and repetition
- Manipulate control statements using the continue and break statements
- Learn about pseudo-random numbers and the Random class
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In this lecture, you will work on your first project for this section. You'll create an application using a Java source file named Proj3_1_LearningPackages. This application should allow a user to determine the total cost after making a purchase of a certain number of courses on an online education platform which we'll call Learning Stars. With Learning Stars, users join and pay a monthly subscription fee and depending on which package they purchase, a certain number of courses are included for that month to which they will have access.
Additionally, they may purchase courses above and beyond what their package includes for a flat rate, also depending on their package level. You should prompt the user for which package they have from the following. Learning package 1 is $10 a month, includes 2 courses per month, and each additional courses $6 each. Learning package 2 is $12 a month, includes 4 courses per month and each additional course on top of the 4 is $4 each. Learning package 3, $15 a month includes 6 courses per month and each additional course on top of the 6 is $3 each.
Your user will initially be prompted for an integer representing package 1, 2 or 3. Then the user should be prompted for how many total courses they enrolled in for that month. Using this information your program should take the package information and number of courses purchased and tell the user their total cost for the month. If they stayed at or under the number of courses included in their monthly package, they only owe the monthly fee. If they went over the allowed package number they must be charged the monthly fee, plus the charge for each additional course at their package's rate.
This project might sound incredibly challenging to you but I strongly encourage you to try this project on your own and not immediately jump into my solution. When you do pause the video, you can sit with a pencil and paper and sketch out the solution and think about it before you start coding. There are no rules saying you must jump right in and start coding. In fact to paraphrase a well-known computer scientist Roy Carlson, "The sooner you start coding, the longer it will take you." As you progress in your abilities and move onto more difficult challenges, you will find that it becomes essential to stop and work on understanding the problem and designing a solution long before you construct the program or program component in code.
Okay without showing you the code let's run this project so you can get an idea of what it's supposed to look like. So, we'll go to run and it will ask the user which package do they want? And let's say they asked for package 3. How many courses did you enroll in? Let's say 6 and it tells you the total cost. So, that should be good. So pause the video and try to complete this project. Come back when you're done or if you need some help. How did that work for you? Were you able to complete this project? Regardless let's work on it together so that you can see how I solved this problem. So, let's first create the file, new, Java class, and we'll call it Proj3_1_LearningPackages. There we go, public static void main(String args). And we're going to use Scanner, so we might as well import it now. Here we go. And I will, a little comment here. So, we need to create the Scanner keyboard = new Scanner(system.in) . We need the int packageNumber, int howManyCourses, int baseCost, int costPerCourse, int numIncluded, and int totalCost. And again there are going to be many, many ways we could do this and this is not the only way to do it, it's just one that's fairly straightforward I think. So, System.out.println ("Which of the packages do you want?
1, 2 or 3?") And then, we will obtain that value using packageNumber = keyboard.nextInt(). And then, here we go, System.out.println("How many courses did you enroll in this month?') And we will do howManyCourses = keyboard.nextInt() again. We' could keep these together if we wanted to. That would be okay. And of course we need to now determine based on the packageNumber and the total number of courses, how much the total is. So, if(packageNumber == 1), we want to do one thing. else if(packageNumber == 2), we want to do another thing. And then implicitly, we know that if it's else, //package 3. So, for package 1, if they have package1, the baseCost = 10, costPerCourse = 6 and the numIncluded = 2. If they have package number 2, the baseCost = 12, the costPerCourse = 4 and the numIncluded = 4. If they have the package 3, the baseCost = 15 but the costPerCourse = 3 and they get a lot more numIncluded = 6. Perfect.
Now that's the end of the if else chain here. And let's do some calculations here, so //calculate total cost. If (howManyCourses > numIncluded), that means they went over. Otherwise, they stayed within it. So, let's see if they stayed within it, is pretty easy. totalCost = baseCost. And then maybe after there we're going to print out, System.out.println("Total cost is $" + totalCost). There we go. Now here's where a lot of the magic happens. If they went over then the totalCost = baseCost + (howManyCourses - numIncluded) * costPerCourse. Since the parentheses will be done first, that will calculate how many over they are. The multiplication is done second so, that calculates that extra cost and then it adds it on to the baseCost. Pretty cool. And we're printing it out of course. So, let's run it and see what we can get. So, right click 'Run' it.
Which are the packages? Let's say package 1, and let's say they roll down 6 courses, says the total cost is $34. Let's see if that makes sense. So, the package 1 includes 2, so that's 4 over. It's $10 baseCost and costPerCourse = 6. So, that's 6*4 is 24 + the base of 10, that's $34. Pretty cool. Let's do it. And maybe check package 3. Package 3, let's say they got 10, it'll be $27. And that makes sense because the cost per course is only $3 and it includes 6. They got 10. So, that's 4 extra at 3 a piece is $12 plus the base of 15, that's 15 + 12, that's 27. Awesome. That's amazing work everyone. Even if you didn't complete the project fully, I'm very glad you gave it your best shot. And if you did finish the project and got it working, that's incredible. I'm so happy about that. Either way, as you progress through the course and add more skills to your toolkit, you'll actually develop even more skills later on and if you come back to this project later on, you may find that it's much easier for you then than it is now. In the next lecture, we have another project, a divisible by three project. Let's check it out.