Project - Divisible By Three

Start course
Overview
Difficulty
Intermediate
Duration
1h 42m
Students
22
Ratings
5/5
starstarstarstarstar
Description

This course explores the fundamental concepts and syntax associated with Control Flow and showcases these with some real-life projects.

Learning Objectives

  • 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 

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

For this project, I would like you to create a file called DivisibleByThree. You will simply prompt the user for an integer and then tell the user whether their number is divisible by three or not. This will help to test your abilities with both the selection control as well as arithmetic operators that you've used before. All right. Without showing you the code, I'd like to run DivisibleByThree or Project3_2DivisibleByThree, whatever you named it. And we're going to run it and see what the input and output look like in the interaction. 

So, let's 'Run' this, and please enter your integer. And I'm going to put, well say, 15 and that should say it is divisible by three because 15 divided by three is five. All right. So, let's run it again, and this time let's do 132, that is divisible by three. Let's try it again, different one. And we'll say 133, not divisible by three. So, give it your best shot. So, pause the video and create the DivisibleByThree file and solve it as best as you can. When you're done or if you need help, come back here and we'll work on it together.

So, how'd that go for you? Were you able to solve this one? I personally think this one is less challenging than the last one. So hopefully, it was straightforward.

Let's write the code together. So, first we need to create a DivisibleByThree Java class file. So, DivisibleByThree. And when you do this obviously, it will add the Java extension to the actual file and the class's name is just DivisibleByThree, of course. All right. So, let's write main, public static void main (String[] args), there we go. And now, let's write some code. We want to import java.util.Scanner so that we can write some code in order to ask the user for input. Scanner keyboard = new Scanner(System.in), and then we're going to create an integer called input, and we will ask the user. So, System.out.println("Please enter your integer") and of course, we'll do input = keyboard.nextInt( ), good. And now, here comes the magical part. This is where we determine is this divisible by three or not. We have to print out the number is divisible by three or the number is not divisible by three. So, if(input % 3 = = 0 ), that means when we divide input by three, it will return the remainder.

And if that remainder is zero, then that means the input is evenly divided by three with no remainder. Here's else. So, we'll say System.out.println(input + "is divisible by 3"). And of course, don't forget the semicolon. And then down here, we have a System.out.println(input + " is NOT divisible by 3"). Awesome. Now let's run it and see what we can get. So, right click, run it, and we get the input here, we'll say 10 and that should be no.  It'll say 10 is NOT divisible by 3. Okay, right click, run again. And let's say nine, which should be divisible by three. Yes, as it is, pretty cool. Great work. Although this was a pretty quick project to complete, that's how projects are sometimes in the real world. 

Not necessarily this easy but a lot of times the solution is something that might be very simple. Then other projects may be extremely challenging. So, there's quite a variety. Up next, you will apply a lot of what you've learned in this section, selection control statements, repetition control statements, and this project even uses the pseudo-random number generator we talked about earlier. So, there's a ton of fun waiting on us. Let's get going.

 

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