Create a file named
assign1.txt
.You will use this file to answer written questions and paste your code.
During this week's lesson, we covered the following topics:
These concepts are core to programming, so it is important for you to consolidate your understanding of them over the next week. Over the course of this class, you will see the endless applications of programming; coding is your creative sandbox in which you build anything you want.
As a recap, assignments are split into four parts: written questions, core assignment, assignment extension, and additional practice. The former two sections are required and the latter two sections are optional.
Good luck and have fun on your first assignment!
What is the difference between variable declaration and initialization?
What are the five most common datatypes used in Java?
Give three correct examples of modulo operator (%
) use.
Which datatypes are the values 3 == 5
, 10
, "hello, world"
, and '&'
?
Explain what the ternary operator is (? :
) and how to use it.
Examine the following program:
xxxxxxxxxx
boolean condition = true;
if (condition == true) {
System.out.println("Condition is true.");
} else {
System.out.println("Condition is false.");
}
How can the code be improved?
Paste your answers into
assign1.txt
Generally, core assignments will be themed in a way that resemble real-world applications and systems. In this assignment, you will be designing a calculator with your new expertise in variables and control flow.
Core assignments are divided into parts called milestones. Instead of trying to pull a dare-devil coding binge in one sitting, these milestones will help you separate your work into manageable sections that can be done sequentially. Ensure that you test each milestone before moving onto the next one.
In your Eclipse editor, make a new project named Assign1
. Then create a Main
class inside the src
folder and place the main method within it. Here is the main method placed inside Main
for reference:
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
//your assignment code will go here
}
}
In the future, assignments will not include this section so that you can get into the habit of creating projects for different programs. However, you can always refer to this assignment specification if you ever forget how to do so.
For now, your calculator will only be able to work with two pre-determined integers at one time. Create two integer variables inside the main method and set them to values that you want to operate on (10
and 5
are my personal choice for this assignment). You can initialize multiple variables in one line using a comma:
xxxxxxxxxx
datatype name1 = some value, name2 = some other value;
In this example, name1
and name2
have the same datatype and are initialized to the values specified after the respective assignment operator (=
). Remember that this is not an actual line of code, but a format for you to replace with real datatypes, values, and names.
You will also want to be able to specify which operation your calculator should perform on these two integers. To do so, create a String
variable initialized to your choice of operation. Because you want your calculator to be standardized, you should only be able to use the following values for this String
:
Remember that the above values are what the String
could be, not that you are initializing four different String
variables. Ensure that all your variable names are properly named so that it is easy to understand what the variables are going to be used for.
The core of your calculator will take the variables defined at the top of your main method and apply the specified operator to them, in the order that they were created. This requires you to apply the correct operator based on the String
value: control flow!
Begin by declaring a chain of if-else statements to cover all the defined operations of the calculator. To make your code conventional by Java standards, we will be using a new concept called the else-if statement. So far, we have only seen this type of control flow:
xxxxxxxxxx
boolean condition = false;
if (condition) {
//do something if condition is true
} else {
//do something if condition is false
}
However, you can also add the else-if statement here:
xxxxxxxxxx
boolean condition1 = false;
boolean condition2 = true;
if (condition1) {
//do something if condition1 is true
} else if (condition2) {
//do something if condition1 is false and condition2 is true
} else {
//do something if both conditions are false
}
You can also drop that last else statement and just leave it like this:
xxxxxxxxxx
boolean condition1 = false;
boolean condition2 = true;
if (condition1) {
//do something if condition1 is true
} else if (condition2) {
//do something if condition1 is false and condition2 is true
}
You can add as many else-if statements to your control flow block as you want, perfect for the many operations of your calculator.
To compare your String variable and String literals (e.g. "add" or "subtract"), use the double equal sign operator (==
) to evaluate if their contents are equal. Recall that with equality and relational operators, their expressions evaluate to booleans, which you can place directly into the conditions of if or else-if statements. We will be strictly using the .equals()
method of Strings later along in the course, but this will work for now.
After you have defined the control flow for your calculator, it's time to implement the calculations and print out the result. For each if/else-if statement, apply the corresponding operation to the two integers at the beginning of your program and store your resulting value inside an intermediate variable for later printing.
IMPORTANT: A concept that we will cover later in the class is something called "scope". The scope of a variable refers where you able to access it and its lifetime. In this case, if you declare or initialize a variable in an if statement, you will not be able to access it outside the if statement. Moreover, that variable will be disposed of as soon as the if statement exits from its code. If you want to keep the result of your calculations, make sure to create the result variable before the if/else-if statements.
Finally, add a print statement at the end of your program to display the result to the console and you're done!
Paste your code into
assign1.txt
Right now, your calculator can only support basic operations between two integers. In this extension, you will be expanding your code base to apply more advanced operations to three integers.
Assignment extensions do not have as much guidance as core assignment, so they will require your creativity and skills to bring to life. Below are the requirements for this extension:
Add support for the modulo operator (%)
Be able to select three integers for the computation and two operations to apply
Follow the rules of PEMDAS for your calculations
3 + 5 * 2
should not evaluate to 16, but instead to 13.Hints:
Paste your code into
assign1.txt
This assignment focused on using arithmetic operators and working with variables. Create a program that prints out an average salary based on the age and occupation of a user, using equality/relational and conditional operators in your control flow. The age and occupation of the user should be variables at the top of the main method, just like the integers in the calculator.
Since your program will likely not have access to data or be able to manipulate it, you can simply hardcode the average salary statistics.