Java Codelab
1.2 Built-in Data Types
¤ Integers
· Literals
Write a literal representing the integer value zero.0
Write a literal representing the long integer value twelve billion.
12000000000l
Write a hexadecimal integer literal representing the value fifteen.
0xF
· Constants
Declare an integer constant, MONTHS_IN_YEAR , whose value is 12 .
final int MONTHS_IN_YEAR = 12 ;
Declare a constant MONTHS_IN_DECADE , whose value is the value of the constant MONTHS_IN_YEAR (already declared) multiplied by 10 .
final int MONTHS_IN_DECADE = MONTHS_IN_YEAR * 10 ;
Declarations
Declare an integer variable named degreesCelsius .int degreesCelsius;
Declare a short variable named patientsAge .
short patientsAge
Declare a long integer variable named grossNationalProduct .
long grossNationalProduct ;
Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear .
int profitStartOfQuarter, cashFlowEndOfYear ;
Declare and initialize the following variables:
· monthOfYear , initialized to the value 11
· companyRevenue , initialized to the value 5666777
· firstClassTicketPrice , initialized to the value 6000
· totalPopulation , initialized to the value 1222333
int monthOfYear = 11;
int companyRevenue = 5666777;
int firstClassTicketPrice = 6000;
int totalPopulation = 1222333;
· Operations
Write an expression that computes the sum of two variables verbalScore and mathScore (already declared and assigned values).verbalScore + mathScore
Given the variables taxablePurchases and taxFreePurchases (already declared and assigned values), write an expression corresponding to the total amount purchased.
taxablePurchases + taxFreePurchases
Write an expression that computes the difference of the variables endingTime and startingTime .
endingTime - startingTime
Given the variables fullAdmissionPrice and discountAmount (already declared and assigned values), write an expression corresponding to the price of a discount admission.
fullAdmissionPrice - discountAmount
Given the variable pricePerCase , write an expression corresponding to the price of a dozen cases.
pricePerCase * 12
Given the variables costOfBusRental and maxBusRiders of type int , write an expression corresponding to the cost per rider (assuming the bus is full). (Do not worry about any fractional part of the expression-- let integer arithmetic, with truncation, act here.)
costOfBusRental / maxBusRiders
Write an expression that computes the remainder of the variable principal when divided by the variable divisor . (Assume both are type int .)
principal % divisor
· Precedence
Write an expression that computes the average of the values 12 and 40 .(12 + 40) / 2
Write an expression that computes the integer average of the int variables exam1 and exam2 (both declared and assigned values).
(exam1 + exam2) /2
¤ Floating Point
· Literals
Write a literal corresponding to the floating point value one-and-a-half.1.5
Write a literal corresponding to the value of the first 6 digits of PI ("three point one four one five nine").
3.14159
·Declarations
Declare a float variable named price .float price ;
Declare a double variable named netWeight .
double netWeight ;
Declare two double variables, one named length with a value of 3.5 and the other named width with a value of 1.55 .
double length = 3.5, width = 1.55 ;
·Operations
Write an expression that computes the sum of two double variables total1 and total2 , which have been already declared and assigned values.total1 + total2
Write an expression that computes the difference of two double variables salesSummer and salesSpring , which have been already declared and assigned values.
salesSummer - salesSpring
You are given two double variables, already declared and assigned values, named totalWeight , containing the weight of a shipment, and weightOfBox , containing the weight of the box in which a product is shipped. Write an expression that calculates the net weight of the product.
totalWeight - weightOfBox
You are given two variables, already declared and assigned values, one of type double , named totalWeight , containing the weight of a shipment, and the other of type int , named quantity , containing the number of items in the shipment. Write an expression that calculates the weight of one item.
totalWeight / quantity
You are given two variables, already declared and assigned values, one of type double , named price , containing the price of an order, and the other of type int , named totalNumber , containing the number of orders. Write an expression that calculates the total price for all orders.
price * totalNumber