CMPS 207- chapter 2

Your page rank:

Total word count: 4875
Pages: 18

Calculate the Price

- -
275 words
Looking for Expert Opinion?
Let us have a look at your work and suggest how to improve it!
Get a Consultant

int degreesCelsius;

declare an integer variable named degreesCelcius

short patientsAge;

declare a short variable named patientsAge

long grossNationalProduct;

declare a long integer variable named grossNationalProduct

int profitStartOfQuarter, cashFlowEndOfYear;

declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear

double netWeight;

declare a double variable name netWeight

float price;

declare a float variable named price

float x;

declare a variable x, suitable for storing values like 3.14159 and 6.02E23

int populationChange;

declare a variable populationChange, suitable for holding numbers like -593142 and 8930522

char c;

declare a character variable named c

double precise = 1.09388641;

declare a variable precise and initialize it to the value 1.09388641

int cardsInHand = 13;

declare an integer variable cardsInHand and initialize it to 13

int monthOfYear = 11;
int companyOfRevenue = 5666777;
int firstClassTicketPrice = 6000;
int totalPopulation = 1222333;

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

double length = 3.5, width = 1.55;

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 temperature = 98.6;

Declare a variable temperature and initialize it to 98.6

long grossNationalProduct;

Declare a long integer variable named grossNationalProduct.

short patientsAge;

Declare a short variable named patientsAge.

drivingAge = 17;

Given an integer variable drivingAge that has already been declared , write a statement that assigns the value 17 to drivingAge.

newRecord = oldRecord;

Given two integer variables oldRecord and newRecord, write a statement that gives newRecord the same value that oldRecord has.

itemp = i;
jtemp = j;
i = itemp;
j = itemp;

Given two int variables , i and j, which have been declared and initialized , and two other int variables , itemp and jtemp, which have been declared , write some code that swaps the values in i and j by copying their values to itemp and jtemp respectively, and then copying itemp and jtemp to j and i respectively.

temp = i;
i = j;
j = temp;

Given three already declared int variables , i, j, and temp, write some code that swaps the values in i and j. Use temp to hold the value of i and then assign j’s value to i. The original value of i, which was saved in temp, can now be assigned to j.

int temp;
temp = firstPlaceWinner;
firstPlaceWinner = secondPlaceWinner;
secondPlaceWinner = temp;

Given two int variables , firstPlaceWinner and secondPlaceWinner, write some code that swaps their values . Declare any additional variables as necessary.

double temp;
temp = bestValue;
bestValue = secondBestValue;
secondBestValue = temp;

Given two double variables , bestValue and secondBestValue, write some code that swaps their values . Declare any additional variables as necessary.

int temp;
temp = pos1;
pos1 = pos2;
pos2 = pos3;
pos3 = pos4;
pos4 = temp;

Four integer variables , pos1, pos2, pos3, pos4 have been declared and initialized . Write the code necessary to "left rotate" their values : for each variable to get the value of the successive variable , with pos4 getting pos1’s value .

val = stdin.nextInt();

Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

System.out.println(message);

Assume that message is a String variable . Write a statement to display its value on standard output .

System.out.println("Today’s Word-Of-The-Day is: " + word);

Assume that word is a String variable . Write a statement to display the message "Today’s Word-Of-The-Day is: " followed by the value of word. The message and the value of word should appear together, on a single line on standard output .

System.out.println("first is " + first + " second = " + second + "\n");

Write a single statement that will print the message "first is " followed by the value of first, and then a space, followed by "second = ", followed by the value of second. Print everything on one line and go to a new line after printing. Assume that first has already been declared as a double and that second has been declared as an int . Assume also that the variables have already been given values

0

Write a literal representing the integer value zero.

12000000000L

Write a literal representing the long integer value twelve billion.

0xf

Write a hexadecimal integer literal representing the value fifteen.

1.5

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

3.14159

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

‘A’

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

‘,’

Write a character literal representing a comma.

‘1’

Write a character literal representing the digit 1.

‘A’

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

‘A’

Write a literal representing the largest character value .

37L

Write a long integer literal that has the value thirty-seven

final int MONTH_IN_YEAR = 12;

Declare an integer constant , MONTHS_IN_YEAR, whose value is 12

final int MONTHS_IN_DECADE = MONTHS_IN_YEAR * 10;

Declare a constant MONTHS_IN_DECADE, whose value is the value of the constant MONTHS_IN_YEAR (already declared ) multiplied by 10

(double) distance / speed

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.

avg = (double)(num1 + num2 + num3) / 3;

Calculate the average (as a double ) of the values contained in the integer variables num1, num2, num3 and assign that average to the double variable avg. Assume the variables num1, num2, and num3 have been declared and assigned values , and the variable avg declared

(double) children / families

Assume that children is an integer variable containing the number of children in a community and that families is an integer variable containing the number of families in the same community. Write an expression whose value is the average number of children per family.

c

Write an expression that computes the sum of two variables verbalScore and mathScore (already declared and assigned values ).

small + large

A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable small. Similarly, the length of the larger one is stored in the variable large. Write a single expression whose value is the length of this wall.

taxablePurchases + taxFreePurchases

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

endingTime – startingTime

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

fullAdmissionPrice – discountAmount

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

pricePerCase * 12

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

costOfBusRental / maxBusRiders

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

principal % divisor

Write an expression that computes the remainder of the variable principal when divided by the variable divisor. (Assume both are type int .

price * totalNumber

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

total1 + total2

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

salesSummer – salesSpring

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

totalWeight – weightOfBox

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 / quantity

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.

x%10

Assume that an int variable x that has already been declared , and initialized to a non-negative value . Write an expression whose value is the last (rightmost) digit of x.

price / 100

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.

price%100

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.

System.out.println(price/100 + " dollars and " + price%100 + " cents");

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".

(12 + 40) / 2

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

(exam1 + exam2) / 2

Write an expression that computes the integer average of the int variables exam1 and exam2 (both declared and assigned values ).

width1 <b> length1 + width2 </b> length2

The dimensions (width and length) of room1 have been read into two variables : width1 and length1. The dimensions of room2 have been read into two other variables : width2 and length2. Write a single expression whose value is the total area of the two rooms.

(small + large) * (small + large)

Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one. The length of all the smaller ones is the same and is stored in the variable small. Similarly, the length of all the larger ones is the same and is stored in the variable large. Write a single expression whose value is the total area of this room. DO NOT any method invocations.

1.0 / 7.0 + 1.0 / 6.0 + 1.0 / 5.0 + 1.0 / 4.0 + 1.0 / 3.0 + 1.0 / 2.0 + 1 + 1.0/8.0

In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + … + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333… and so on. Write an expression whose value is the 8th harmonic number.

hn + 1.0 / (n + 1)

In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + … + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333… and so on. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.

(double)(tickets1 + tickets2 + tickets3) / (class1 + class2 + class3)

Three classes of school children are selling tickets to the school play. The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables :tickets1, tickets2, tickets3 and class1, class2, class3. Write an expression for the average number of tickets sold per school child.

gradAge = matricAge + 4;

Given two integer variables matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge.

bridgePlayers += 4;

Given an integer variable bridgePlayers, write a statement that increases the value of that variable by 4.

profits *= 10;

Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.

total += amount;

Write a statement that increases the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.

strawsOnCamel ++;

Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1.

timer –;

Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1.

""

Write a String constant that is the empty string .

"f"

Write a String constant consisting of exactly one character — any character will do.

"!!!!!"

Write a String constant consisting of exactly 5 exclamation marks.

String title = new String();

Write the declaration of a String variable named title.

String mailingAddress = new String();

Declare a String variable named mailingAddress.

String win = new String();
String place = new String();
String show = new String();

Write the declaration of three String variables named win, place, and show.

String oneSpace = new String (" ");

Declare a String variable named oneSpace, and initialize it to a String consisting of a single space.

String empty = new String();

Declare a String variable named empty, and initialize it to the empty String .

String background = new String ( "white");
String selectionColor = new String ( "blue" );

Write the declaration of two String variable named background and selectionColor and initialize them to "white" and "blue" respectively.

String foreground = new String("black");

Write the declaration of a String variable named foreground and initialize it to "black".

foreground = "red";

Assume that the String variable named foreground has already been declared . Assign it the value "red".

text = "";

Assume that theString variable named text has already been declared . Assign to it the empty string .

String temp = new String();
temp = s1;
s1 = s2;
s2 = temp;

There are two String variables , s1 and s2, that have already been declared and initialized . Write some code that exchanges their values . Declare any other variables as necessary.

"http://" + address

Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable ‘s String value . So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

areaCode + "-" + exchange + "-" + lastFour

Given three int variables that have been declared and given values , areaCode, exchange, and lastFour, write a String expression whose value is the String equivalent of each these variables joined by a single hyphen (-) So if areaCode, exchange, and lastFour, had the values 800, 555, and 1212, the expression ‘s value would be "800-555-1212". Alternatively, if areaCode, exchange, and lastFour, had the values 212, 867 and 5309 the expression ‘s value would be "212-867-5309".

octet1 + "." + octet2 + "." + octet3 + "." + octet4

Given four int variables that have been declared and given values , octet1, octet2, octet3, and octet4, write a String expression whose value is the String equivalent of each these variables joined by a single period (.) So if octet1, octet2, octet3, and octet4, had the values 129, 42, 26, and 212 the expression ‘s value would be "129.42.26.212". Alternatively, if octet1, octet2, octet3, and octet4, had the values 192, 168, 1and 44 the expression ‘s value would be "192.168.1.44".

firstName + " " + middleName + " " + lastName

Given three String variables that have been declared and given values , firstName, middleName, and lastName, write an expression whose value is the values of each these variables joined by a single space. So if firstName, middleName, and lastName, had the values "Big", "Bill", and "Broonzy", the expression ‘s value would be "Big Bill Broonzy". Alternatively, if firstName, middleName, and lastName, had the values "Jerry", "Lee", and "Lewis", the expression ‘s value would be "Jerry Lee Lewis".

"(" + word + ")"

Given a String variable word, write a String expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the String "(sadly)"

gold + "\n" + silver + "\n" + bronze

Given three String variables that have been declared and given values , gold, silver, and bronze, write an expression whose value is the values of each these variables joined by a newline character . So if gold, silver, and bronze, had the values "Arakawa", "Cohen", and "Slutskaya", the expression , if it were printed would have the names "Arakawa", "Cohen", and "Slutskaya" each appearing on a separate line. (Do NOT print anything in this exercise: just write the expression .)

prefix + suffix

Write an expression that concatenates the String variable suffix onto the end of the String variable prefix .

sentence.length()

Given a String variable named sentence that has been initialized , write an expression whose value is the number of characters in the String referred to by sentence.

sentence.length() – 1

Given a String variable named sentence that has been initialized , write an expression whose value is the index of the very last character in the String referred to by sentence.

"CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007’\\’\"TOOMUCH!".length()

Write an expression whose value is the number of characters in the following String : "CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007’\\’\"TOOMUCH!" Suggestion: copy/paste the above String into your code– it’s too crazy to try to copy it by typing.

str.charAt(0)

Given the String variable str, write an expression that evaluates to the character at index 0 of str.

name.charAt(0)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is the first character of the value of name . So if the value of name were "Smith" the expression ‘s value would be ‘S’.

name.charAt(1)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is the second character of the value of name . So if the value of name were "Smith" the expression ‘s value would be ‘m’.

name.charAt(4)

Write an expression that whose value is the fifth character of the String name .

sentence.charAt(sentence.length() – 1)

Given a String variable named sentence that has been initialized , write an expression whose value is the the very last character in the String referred to by sentence.

name.charAt(name.length() – 1)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is the last character of the value of name . So if the value of name were "Blair" the expression ‘s value would be ‘r’.

name.substring(0,1)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is a String containing the first character of the value of name . So if the value of name were "Smith" the expression ‘s value would be "S".

word.substring(0,3)

Assume that word is a variable of type String that has been assigned a value . Write an expression whose value is a String consisting of the first three characters of the value of word. So if the value of word were "dystopia" the expression ‘s value would be "dys".

name.substring(1,2)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is a String containing the second character of the value of name . So if the value of name were "Smith" the expression ‘s value would be "m".

s.substring(2,10)

Write an expression that results in a String consisting of the third through tenth characters of the String s.

name.substring(name.length() – 1,name.length())

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is a String containing the last character of the value of name . So if the value of name were "Smith" the expression ‘s value would be "h".

word.substring(word.length() – 3,word.length())

Assume that word is a variable of type String that has been assigned a value . Write an expression whose value is a String consisting of the last three characters of the value of word. So if the value of word were "biggest" the expression ‘s value would be "est".

address.indexOf("Avenue")

Given the String variable address, write an expression that returns the position of the first occurrence of the String "Avenue" in address.

"" + firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0)

Given three String variables that have been declared and given values , firstName, middleName, and lastName, write an expression whose value is the initials of the three names : the first letter of each, joined together. So if firstName, middleName, and lastName, had the values "John", "Fitzgerald", and "Kennedy", the expression ‘s value would be JFK". Alternatively, if firstName, middleName, and lastName, had the values "Franklin", "Delano", and "Roosevelt", the expression ‘s value would be "FDR".

pos = line.indexOf(‘,’);
clause = line.substring(0,pos);

Write a sequence of statements that finds the first comma in the String line, and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos, as well as the variables line and clause, have already been declared .

int start = sentence.indexOf(" ");
int end = sentence.indexOf(" ", 0);
firstWord = sentence.substring(0,sentence.indexOf(‘ ‘));

Assume that sentence is a variable of type String that has been assigned a value . Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , firstWord, also of type String . Write the statements needed so that the first word of the value of sentence is assigned to firstWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord.

given.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + "."

Assume that given , middle and family are three variables of type String that have been assigned values . Write an expression whose value is a String consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name . So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression ‘s value would be "J.F.K.".

System.out.println(count);

Given an integer variable count, write a statement that displays the value of count on the screen. Do not display anything else on the screen — just the value of count

System.out.println(fraction);

Given a floating point variable fraction, write a statement that displays the value of fraction on the screen. Do not display anything else on the screen– just the value of fraction

System.out.println(num + " " + cost + "\n");

Two variables , num and cost have been declared and given values : num is an integer and cost is a double . Write a single statement that outputs num and cost to standard output . Print both values (num first, then cost), separated by a space on a single line that is terminated with a newline character . Do not output any thing else.

System.out.println("i="+i+" f="+f) ;

The exercise instructions here are LONG — please read them all carefully. If you see an internal scrollbar to the right of these instructions, be sure to scroll down to read everything. Given an integer variable i and a floating-point variable f, that have already been given values , write a statement that writes both of their values to standard output in the following format: i=value -of-i f=value -of-f Thus, if i’s value were 25 and f’s value were 12.34, the output would be: i=25 f=12.34 But you don’t know what i’s value and f’s value are. They might be 187 and 24.06. If that’s what their values are, the output from your statement should be: i=187 f=24.06 On the other hand, they might be 19 and 2.001. If that’s what their values are, the output from your statement should be: i=19 f=2.001 Remember: you are GIVEN i and f– that means they are already declared and they already have values ! Don’t change their values by assigning or initializing them! Just print them out the way we have shown above. Just write one statement to produce the output . Remember: in your output you must be displaying both the name of the variable (like i) and its value .

firstWord = stdin.next();

Write a statement that reads a word from standard input into firstWord. Assume that firstWord. has already been declared as a String variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

temperature = stdin.nextDouble();

Write a statement that reads a floating point value from standard input into temperature. Assume that temperature. has already been declared as an double variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

name = stdin.next();
age = stdin.nextInt();
System.out.println("The age of " + name + " is " + age);

Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE" on a line by itself, where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70" on a line by itself. There should NOT be a period in the output .

name = stdin.next();
System.out.println("Greetings, " + name);

Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Assume also that stdin is a variable that references a Scanner object associated with standard input Write some code that reads a value into name then prints the message "Greetings, NAME " on a line by itself, where NAME is replaced the value that was read into name . For example, if your code read in "Rachel" it would print out "Greetings, Rachel" on a line by itself.

name = stdin.next();
System.out.println("Greetings, " + name + "!!!");

Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa"). Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads a value into name then prints the message "Greetings, NAMEVALUE!!!" on a line by itself where NAMEVALUE is replaced the value that was read into name . For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!" on a line by itself.

name1 = stdin.next();
name2 = stdin.next();
name3 = stdin.next();
System.out.println(name1 + ", " + name2 + " and " + name3);
System.out.println(name1 + ", " + name3 + " and " + name2);
System.out.println(name2 + ", " + name1 + " and " + name3);
System.out.println(name2 + ", " + name3 + " and " + name1);
System.out.println(name3 + ", " + name2 + " and " + name1);
System.out.println(name3 + ", " + name1 + " and " + name2);

Three business partners are forming a company whose name will be of the form "Name1, Name2 and Name3". However, they can’t agree whose name should be first, second or last. Help them out by writing code that reads in their three names and prints each possible combination exactly once, on a line by itself (that is, each possible combination is terminated with a newline character ). Assume that name1, name2 and name3 have already been declared and use them in your code. Assume also that stdin is a variable that references a Scanner object associated with standard input. For example, if your code read in "Larry", "Curly" and "Moe" it would print out "Larry, Curly and Moe", "Curly, Larry and Moe", etc., each on a separate line.

Share This
Flashcard

More flashcards like this

NCLEX 10000 Integumentary Disorders

When assessing a client with partial-thickness burns over 60% of the body, which finding should the nurse report immediately? a) ...

Read more

NCLEX 300-NEURO

A client with amyotrophic lateral sclerosis (ALS) tells the nurse, "Sometimes I feel so frustrated. I can’t do anything without ...

Read more

NASM Flashcards

Which of the following is the process of getting oxygen from the environment to the tissues of the body? Diffusion ...

Read more

Unfinished tasks keep piling up?

Let us complete them for you. Quickly and professionally.

Check Price

Successful message
sending