Some problems for your consideration. Variable names Which of the following may be used as variable...

18
Some problems for Some problems for your consideration your consideration

Transcript of Some problems for your consideration. Variable names Which of the following may be used as variable...

Page 1: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

Some problems for your Some problems for your considerationconsideration

Page 2: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

Variable namesVariable names

Which of the following may be used Which of the following may be used as variable names in Java?as variable names in Java? rate1rate1 1stPlayer1stPlayer myprogrammyprogram switchswitch longlong TimeLimitTimeLimit numberOfWindowsnumberOfWindows

Page 3: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

Variable namesVariable names

Can a Java program have two Can a Java program have two different variables named different variables named numbernumber and and NumberNumber??

Page 4: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

VariablesVariables

Give the declaration for two variables Give the declaration for two variables called called countcount and and distancedistance. count is . count is of type int and is initialized to zero. of type int and is initialized to zero. distance is of type double and is distance is of type double and is initialized to 1.5.initialized to 1.5.

Page 5: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

ExpressionsExpressions

What is the output of the following What is the output of the following program lines:program lines:

double number = (1/3) * 3;double number = (1/3) * 3;

System.out.println( "(1/3) * 3 is equal to " System.out.println( "(1/3) * 3 is equal to " + number );+ number );

Page 6: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

ExpressionsExpressions

What is the output produced by the What is the output produced by the following code:following code:int result = 11;int result = 11;

result = result / 2;result = result / 2;

System.out.println( "result is " + result );System.out.println( "result is " + result );

Page 7: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

OutputOutput

What is the output produced by the What is the output produced by the following:following:

String verbPhrase = "is money";String verbPhrase = "is money";

System.out.println( "Time" + System.out.println( "Time" + verbPhrase );verbPhrase );

Page 8: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

OutputOutput

What is the output of the following:What is the output of the following:

System.out.println( "2 + 2 = " + (2 + 2) );System.out.println( "2 + 2 = " + (2 + 2) );

System.out.println( "2 + 2 = " + 2 + 2 );System.out.println( "2 + 2 = " + 2 + 2 );

Page 9: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

CommentsComments

What are the two kinds of comments What are the two kinds of comments in Java?in Java?

Page 10: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

CommentsComments

What is the output produced by the What is the output produced by the following Java code?following Java code?

System.out.println( "Hello" );System.out.println( "Hello" );

//System.out.print( "Mr. or Ms. " );//System.out.print( "Mr. or Ms. " );

System.out.println( "Student" );System.out.println( "Student" );

Page 11: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

CouponsCoupons

The skeeball machines on the boardwalk in The skeeball machines on the boardwalk in Wildwood output coupons according to how Wildwood output coupons according to how well you play the game.well you play the game. You can redeem 10 coupons for a candy bar or 3 You can redeem 10 coupons for a candy bar or 3

coupons for a gumball. You prefer candy bars to coupons for a gumball. You prefer candy bars to gumballs.gumballs.

Write a program that defines a variable initially Write a program that defines a variable initially assigned to the number of coupons you win. Next, assigned to the number of coupons you win. Next, the program should output how many candy bars the program should output how many candy bars and gumballs you can get if you spend all of your and gumballs you can get if you spend all of your coupons on candy bars first, and any remaining coupons on candy bars first, and any remaining coupons on gumballs.coupons on gumballs.

Page 12: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

StringsStrings

What output is produced by the What output is produced by the following:following:String s = "Hello" + "Joe";String s = "Hello" + "Joe";

System.out.println( s );System.out.println( s );

Page 13: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

StringsStrings

Write Java statements that will cause Write Java statements that will cause the following to be written to the the following to be written to the screen:screen:

One two buckle your shoe.One two buckle your shoe.

Three four shut the door.Three four shut the door.

Page 14: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

StringsStrings

What is the difference between What is the difference between System.out.println and System.out.println and System.out.print?System.out.print?

Page 15: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

StringsStrings

What is the output produced by the What is the output produced by the following:following:

System.out.println( 2 + " " + 2 );System.out.println( 2 + " " + 2 );

System.out.println( 2 + 2 );System.out.println( 2 + 2 );

Page 16: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

Sum, difference, and productSum, difference, and product

Write a program that reads in two Write a program that reads in two integers (typed from the keyboard) integers (typed from the keyboard) and outputs their sum, difference, and outputs their sum, difference, and product.and product.

Page 17: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

The cost of gasThe cost of gas

A car is used for commuting. Write a A car is used for commuting. Write a program that takes as input the program that takes as input the distance of the commute in miles, distance of the commute in miles, the car’s fuel consumption rate in the car’s fuel consumption rate in miles per gallon, and the price of a miles per gallon, and the price of a gallon of gas. The program should gallon of gas. The program should then output the cost of the commute.then output the cost of the commute.

Page 18: Some problems for your consideration. Variable names Which of the following may be used as variable names in Java? Which of the following may be used.

Make changeMake change

Write a program that determines the change to Write a program that determines the change to be dispensed from a vending machine. An be dispensed from a vending machine. An item in the machine can cost between 25 item in the machine can cost between 25 cents and a dollar, in 5-cent increments, and cents and a dollar, in 5-cent increments, and the machine accepts only a single dollar bill the machine accepts only a single dollar bill to pay for the item.to pay for the item.

For example, a possible sample dialog might be:For example, a possible sample dialog might be:Enter price of an item: 45Enter price of an item: 45

You bought an item for 45 cents and gave me You bought an item for 45 cents and gave me a dollar so your change is:a dollar so your change is:

2 quarters2 quarters0 dimes, and0 dimes, and1 nickel1 nickel