Tuesday, October 4, 2011

Java Programming Helper


Primitive_Types
Integers
Literals – 1
1.  Write a literal representing the integer value zero.
No example, just an analogy:
The literal representing the integer value twenty-one is
21
This is one of those problems where the answer is much, much simpler than the instructions!

0

Constants

1.  Declare an integer constant, MONTHS_IN_YEAR, whose value is 12.

final int MONTHS_IN_YEAR = 12 ;

2.  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 – 1
1.  Declare an integer variable named degreesCelsius.

int degreesCelsius ;


2.  Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.

int profitStartOfQuarter, cashFlowEndOfYear ;

3.  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 – 1

1.  Write an expression that computes the sum of two variables verbalScore and mathScore (already declared and assigned values).
Example:  So, if the value of verbalScore were 500 and the value of mathScore were 450 then the value of your expression would be 950.

verbalScore + mathScore

2.  Given the variables taxablePurchases and taxFreePurchases (already declared and assigned values), write an expression corresponding to the total amount purchased.

taxablePurchases + taxFreePurchases

3.  Write an expression that computes the difference of the variables endingTime and startingTime.

endingTime – startingTime

4.  Given the variables fullAdmissionPrice and  discountAmount (already declared and assigned values), write an expression corresponding to the price of a discount admission.

fullAdmissionPrice - discountAmount

5.  Given the variable pricePerCase, write an expression corresponding to the price of a dozen cases.

pricePerCase * 12

6.  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

7.  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

1.  Write an expression that computes the average of the values 12 and  40.

(12 + 40) / 2

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-2

1.  Write a floating point literal corresponding to the value zero.

0

2.  Write a literal corresponding to the floating point value one-and-a-half.

1.5

3.  Write a literal corresponding to the value of the first 6 digits of PI ("three point one four one five nine").

3.14159

Declarations-2

1.  Declare a float variable named price.

float price ;

2.  Declare a double variable named netWeight.

double netWeight ;

3.  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-2

1.  Write an expression that computes the sum of two double variables total1 and total2, which have been already declared and assigned values.

total1 + total2

2.  Write an expression that computes the difference of two double variables salesSummer and salesSpring, which have been already declared and assigned values.

salesSummer - salesSpring

3.  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

4.  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

5.  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

Boolean
Literals-3

Declarations-3

1.  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


Character_Type
Literals-4

1.  Write a character literal representing the (upper case) letter A.

'A'

2.  Write a character literal representing a comma.

','

3.  Write a character literal representing the digit  1 .

'1'

4.  Write a literal representing the character whose unicode value is  65 .

(char)65

Declarations-4

1.  Declare a character variable named c.

char c ;

ConversionsAndCasting

1.  Given two integer variables distance and speed, write an expression that divides distance by speed using floating point arithmetic, i.e. a fractional result should be produced.
Example:  So, if the value of distance were 25 and the value of speed were 10 then the value of the expression would be 2.5.

(double) distance / speed







2 comments:

  1. You can clear many of your doubts regarding Literals in Java through Merit Campus, visit: http://java.meritcampus.com/core-java-topics/literals-in-java, http://java.meritcampus.com/core-java-topics/floating-point-literals-in-java

    Not only Literals, we also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: http://java.meritcampus.com/core-java-topics/ to know more.

    ReplyDelete