Skip to content

Are you an adult?

Write a simple Java program

  • Project
  • 5 hints
  • 8 of 8 in this chapter

Project time!

This is a project section. These operate a bit differently than lessons or quizzes, as they allow you to use what you have learned. We will give a few rules for you to follow, and you will program accordingly. Don't worry, there's no penalty or problem if you mess up. It's all about learning. At the end of this project, you can see the answer equivalent.

What will I do?

For this project, your goal is to make a program that gives different answers when you change the variables. Seems easy, right? Here are the instructions:

  • Create a variable for your age
  • Create a variable for your name
  • Have the code say "Hi" to you
  • Create a conditional that checks if you are an adult (18) and prints the result
  • Create a loop that prints your name age times

You can always mess with these however you like. Feel free to experiment and play around! Our answer will be following the above instructions. Go ahead and get programming!

Hints

Hint 1How do I create a variable?

To create a variable, list the type you want it to be, give it a name, and set it to a value (if needed).

Hint 2What is a conditional?

Remember, a conditional is a simple if/else statement. In this case, if you are 18 or older, you are an adult. Else, you are not an adult.

Hint 3How do I print the result?

Remember, use System.out.println(); to print out your result. Put whatever you want to print inside the parenthesis.

Hint 4How do I create a for loop?

You can create a for loop like so: for(int i = 0; i < max; i++) {} max should be however times you want the loop to run. Put the code to loop inside the curly braces.

Hint 5How do I create a while loop?

You can create a while loop like so: while(condition) {} condition must be something that can be true or false, like i < age. Put what you want to loop inside the curly braces.

Solution

Java
public Main {
 public static void main(String[] args) {
  int age = 16;
  String name = "Greg";

  System.out.println("Hi" + name);

  if(age < 18) {
   System.out.println("You are not an adult.");
  } else {
   System.out.println("You are an adult.");
  }

  // While Loop
  int a = 0;
  while(a < age) {
   System.out.println(a);
   a++;
  }

  // For Loop
  for(int i = 0; i < age; i++) {
   System.out.println(a);
  }
 }
}

Give the project a try first.