Average of Three Project

Start course
Overview
Difficulty
Intermediate
Duration
2h 7m
Students
47
Ratings
5/5
starstarstarstarstar
Description

This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.

Learning Objectives

  • Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
  • Learn about arithmetic, relational, and logical operators
  • Understand how to obtain input from the user of our programs

Intended Audience

  • 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

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In this section, we've learned a lot of the fundamentals for working with Java. We've worked quite a bit with values, variables, constants, and operators, as well as learning how to print data to the user and obtain data from the user. This lecture covers your first dedicated project for the section. What I'd like you to do is fairly simple. You'll create a file in our Section 2 project named Proj2_1_AverageOfThree, and then write out the main method skeleton. For this project, you will then prompt the user for three different real numbers, and you will print out their average, that is their mean value, to the console. As a reminder, the average, specifically the mean value, is a measure of central tendency and is calculated by taking the count of the values that you have and dividing their sum by that count.

For example, if you have 100 numbers, you'd sum them up and then divide by 100. Pretty simple, right? As a hint, you can use parentheses in Java just like you would in mathematics. All right, let's take a look at this project, AverageOfThree, in action. If we right click it, go to 'Run' project, it asks us to enter three numbers. So, I'll put 100, we'll say 125, and 150. Then I hit 'Enter' and the average is 125. Pretty awesome. So, pause the video and try your best to complete this challenge. Come back when you're done or if you need some help. How did that go for you? Were you able to complete this first section project? Let's work on it together. So now, we will first create by right-clicking 'New', 'Java', and then we will call the class Proj2_1_AverageOfThree. There's our class, and notice you have to use the underscores if you want to separate these out really well.

You can't have spaces, and you shouldn't put minus signs either, because it will think you're trying to do arithmetic and that would be confusing. In order to get user input, we need the java.util.Scanner. And then, we have our main method. Here we go. There's the end of main. And we want our Scanner. We'll call keyboard... (System.in), and we need three doubles. So, we'll say double num1; double num 2; double num3;, and then we need something to hold the average once we're done. Let's ask for the values. Please enter three numbers. num1 = keyboard.nextDouble(). And then, we can go straight into this num2 keyboard.nextDouble(), and num3 = keyboard.nextDouble(). And we want to consume the new line, keyboard.nextLine(), that should consume new line or new lines. So, that's our good habit to get into.

And then, average equals... technically, since we're not pulling in a next line or a new line string, we don't really have to use the next line to consume the new line character but it's a good habit to get into. So, that's why I'm having us do it. Now, println("Average is: " + average). So, we calculated the average probably as you expected. We have the double here, we have to add the values together first in parentheses because if we left the parentheses off, it would just divide the last value by three and then add it to the other two. So here, we add all three of the values that are entered, then divide by 3.0, which is also a double. You could just put three in this particular case because this whole thing would be a double divided by an integer which would be a double, and then it starts in a double. So, that's fine.

But just to keep it consistent, we've got all doubles here. So, let's run this project, right-click, and then we go to 'Run 'Proj2_1_Averag....main()'.' All right. Please enter three numbers. So, I will put, let's say, 15.5, 12.5, and 10.25, and hit 'Enter'. Here you go. It says the average is 12.75. You can verify that for yourself, but I think you'll come up with the same solution. Awesome. So, we add the three numbers together and then divide the sum by three. Some of the tricky parts of this project were figuring out what data types to use, and that you had to use parentheses around the numbers you were adding. Nice work, everyone. Even if you didn't complete it or get the correct solution, that's okay. Over time, you will become more familiar with the material and get even better with it. Next up is another project - a Mad Libs clone. I'll see you there.

 

About the Author
Students
293
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics