Chapter 2 Part 1 Recitation – Naming Convention, Data Types, and Literals and Arithmetic Operators
Introduction
In this this part of this recitation, we will working on the topics of Naming Convention, Data Types, and Literals by using day-to-day situations and see how we would convert them into Java code. In the second part we will work on Arithmetic operators by creating simple programs that perform some kind of useful calculation.
Part 1 – Naming Convention, Data Types, and Literals
A very important aspect of creating a great program is the ability of defining variables that not only follow the naming convention, but also have meaningful names, so that any one that reads the variable name can have a pretty good idea what that variable is intended to hold.
Examples:
int temp = 7;
int a = 50;
double x = 170;
are bad variable names, since we have no idea what they represent.
numberOfDaysInAWeek = 7;
numberOfStudents = 50;
personWeight = 170;
are better names.
Pay attention the naming convention: they are written in “camelCaseStyle” instead of “camel_case_style”.
Activity #1:
Based on the variable types that we have studied in class (byte, int, char, float, double, String, boolean, long), Create Java variables (names and their types) to be used to hold the answers for the following questions:
Question: |
Variable Type | Variable Name | Example of value |
How many cars does your family have? | byte | numberOfCars | 4 |
What is the value of PI? | |||
Is it raining outside? | |||
What is the initial of your middle name? | |||
What is your last name? | |||
How many days we have in 10 years? | |||
Do you want fries in your order? | |||
Type “y” for Yes and “n” for No | |||
How much was the restaurant bill tonight? | |||
Have you called Uber already? | |||
How many hours do I need to study per week? | |||
How many bits do we have in 2 Bytes? |
Activity #2:
AFTER checking your answer from question 1 above with your Lab-TA, go to NetBeans and create a program that define those variables and print them in the terminal window. See example for the first question below:
byte numberOfCars = 4;
System.out.println(“My family has a total of “ + numberOfCars + “ cars”);
The code output would be:
My family has a total of 4 cars
Part 2 – Arithmetic Operators
You are about to create simple programs below. Remember to define variables in the same way you just did in the first part of this assignment to hold the required data and final calculation. Use camelCase and meaningful variable names!
Activity #3:
You have just moved to Brazil and the currency exchange there is about $1.00 is equivalent to R$ 3.75. Create a small program that for any given value in dollars, it outputs how much reals it would be. Your program should print the following (imagine that you have given $100.00).
100.00 dollars is worth 375.00 reals.
Activity #4:
The volume of a sphere of radius r is given by the following equation
Volume = 4/3*pi*r3
Where r3 is r * r * r
Create a program that computes the volume of a sphere of radius 5. And prints the following output
The volume of a sphere of radius 5 feet is 523.6 cubicle-feet
Activity #5:
A local News Paper published that a person ran the marathon in just 12345 seconds!!!! Hum, I would like to convert this non-sense number to something more practical, i.e., I would like to know how many hours, minutes and seconds he took to complete the marathon.
Using integer division and remaining operators, create a program that will do that conversion for you and provide the following output:
The man has taken 3 hours, 25 minutes, and 45 seconds to finished the race!