CSC 110 Assignment 3 (2.9-2.15 and 3.1-3.2)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

"(" + word + ")"

We hope you never have to encounter anything like the following expression in a program, but consider it for a moment and indicate its value: ( /* 5 + 3 */ - 9 + 6/*8*//2 ) //*+4*/ -10 3 -7 -6 5 -8

-6

Which of the following lines does NOT consist of (a) valid, though boastful, comment(s)? // /* This is a */ First Rate Program //**// This is a //**// First Rate Program //**// //* This is a //*// First Rate Program //*// /* This is a //*// First Rate Program //*//

/* This is a //*// First Rate Program //*//

A comment starts with what characters?

//

The _______ class allows non-GUI applications to interact with users using dialog boxes.

JOptionPane

Given an int variable datum that has already been declared, write a few statements that read an integer value from standard input into this variable.

Scanner keyboard = new Scanner(System.in); datum = keyboard.nextInt();

Data returned by JOptionPane is always of the _______ type (excluding yes-or-no questions).

String

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

String empty = "";

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

String foreground = "black";

Declare a String variable named mailingAddress.

String mailingAddress;

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

String oneSpace = " ";

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.

String temp = s1; s1=s2; s2=temp;

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

String win, place, show;

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

address.indexOf("Avenue")

NOTE: in mathematics, the square root of a negative number is not real; in Java therefore, passing such a value to the square root function returns a value known as NaN (not-a-number). Given a double variable named areaOfSquare write the necessary code to read in a value, the area of some square, into areaOfSquare and print out the length of the side of that square. HOWEVER: if any value read in is not valid input, just print the message "INVALID". ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

areaOfSquare = stdin.nextDouble(); double sqrt = Math.sqrt(areaOfSquare); if(Double.isNaN(sqrt)){ System.out.print("INVALID"); } else { System.out.print(sqrt); }

Given the char variable c, write an expression that is true if and only if the value of c is not the space character.

c != ' '

Assume that c is a char variable has been declared and already given a value. Write an expression whose value is true if and only if c is a newline character.

c == '\n'

Assume that c is a char variable has been declared and already given a value. Write an expression whose value is true if and only if c is a tab character.

c == '\t'

NOTE: in mathematics, division by zero is undefined. So, in Java, division by zero is always an error. Given a int variable named callsReceived and another int variable named operatorsOnCall write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do). HOWEVER: if any value read in is not valid input, just print the message "INVALID". ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

callsReceived = stdin.nextInt(); operatorsOnCall = stdin.nextInt(); if(operatorsOnCall != 0) { System.out.print(callsReceived/operatorsOnCall); } else { System.out.print("INVALID"); }

The text of a comment is checked by the compiler for accuracy. must appear in the first line of the program. is printed out when the program runs. can be anything the programmer wants to write.

can be anything the programmer wants to write.

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

firstName + " " + middleName + " " + lastName

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.

firstWord = stdin.nextLine();

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

foreground = "red";

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

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

Working overtime is defined as having worked more than 40 hours during the week. Given the variable hoursWorked, write an expression that evaluates to true if the employee worked overtime.

hoursWorked>40

Write an if/else statement that compares the variable age with 65, adds 1 to the variable seniorCitizens if age is greater than or equal to 65, and adds 1 to the variable nonSeniors otherwise.

if(age >= 65) seniorCitizens += 1; else nonSeniors +=1;

Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000.

if(goodsSold > 500000) bonus = 10000;

Assume that the variables gpa, deansList and studentName, have been declared and initialized. Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

if(gpa > 3.5){ deansList = deansList +1; System.out.println(studentName); }

Assume that a boolean variable workedOvertime has been declared, and that another variable, hoursWorked has been declared and initialized. Write a statement that assigns the value true to workedOvertime if hoursWorked is greater than 40 and false otherwise.

if(hoursWorked > 40) workedOvertime = true; else workedOvertime = false;

Assume that isIsosceles is a boolean variable, and that the variables isoCount,triangleCount, and polygonCount have all been declared and initialized. Write a statement that adds 1 to each of these count variables (isoCount,triangleCount, andpolygonCount) if isIsosceles is true.

if(isIsosceles == true) { isoCount +=1; triangleCount +=1; polygonCount += 1; }

Assume that a boolean variable isQuadrilateral has been declared, and that another variable, numberOfSides has been declared and initialized. Write a statement that assigns the value true if numberOfSides is exactly 4 and false otherwise.

if(numberOfSides==4) isQuadrilateral = true; else isQuadrilateral = false;

Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90.

if(outsideTemperature > 90) shelfLife = shelfLife - 4;

Write a conditional that assigns the boolean value true to the variable fever if the variable temperature is greater than 98.6.

if(temperature > 98.6) fever = true;

Write a conditional that multiplies the value of the variable pay by one-and-a-half if the value of the boolean variable workedOvertime is true.

if(workedOvertime==true) pay *= 1.5;

The import statement needed to use JOptionPane is

import javax.swing.JOptionPane;

Which of the following lines contains a valid, though not necessarily accurate, comment? int twoPi = 3.14159; /* holds the value of two times pi */ int twoPi = 2*3.14159; /* holds the value of two times pi //* int twoPi = 2*3.14159; / / *holds the value of 6 //* double twoPi = 2*3.14159; /* // holds the value of two time pi */ [comment] //

int twoPi = 3.14159; /* holds the value of two times pi */

Which comment below is the most helpful? int volume; // declare an int int volume; // declare volume int volume; // declare volume to be an int variable int volume; // size of trunk in cubic feet

int volume; // size of trunk in cubic feet

Given a String variable named line1 and given a Scanner reference variable stdin that has been assigned a reference to a Scanner object, read the next line from stdin and save it in line1. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

line1 = stdin.nextLine();

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(); age = stdin.nextInt(); System.out.println("The age of " + name + " is " + age);

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(0)

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(1)

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

name.charAt(4)

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.charAt(name.length()-1)

Given the variables numberOfMen and numberOfWomen, write an expression that evaluates to true if the number of men is greater than or equal to the number of women.

numberOfMen >= numberOfWomen

Write an expression that evaluates to true if the value of the integer variable numberOfPrizes is divisible (with no remainder) by the integer variable numberOfParticipants. Assume that numberOfParticipants is not zero.

numberOfPrizes % numberOfParticipants == 0

8. Cookie Calories A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servings in the bag and that a serving equals 300 calories. Write a program that lets the user enter the number of cookies he or she actually ate and then reports the number of total calories consumed.

package javacookies; import java.util.Scanner; public class JavaCookies { public static void main(String[] args) { int cookieCount; Scanner keyboard = new Scanner(System.in); System.out.println("Enter number of cookies eaten:"); cookieCount = keyboard.nextInt(); int servingSize = 40/10; double caloriesPerCookie = 300/servingSize; double totalCalories = cookieCount * caloriesPerCookie; System.out.println("Your calorie intake was: " + totalCalories + " calories"); } }

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

prefix + suffix

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.

sentence.charAt(sentence.length()-1)

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

The JOptionPane method that gets input from the user is

showInputDialog

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.

temperature = stdin.nextDouble();

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

text = "";

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.

val = stdin.nextInt();

Write an expression that evaluates to true if the value of the integer variable widthOfBox is not divisible by the value of the integer variable widthOfBook. Assume that widthOfBook is not zero. ("Not divisible" means has a remainder.)

widthOfBox % widthOfBook != 0

Write an expression that evaluates to true if and only if the value of the integer variable x is equal to zero.

x==0

Write an expression that evaluates to true if and only if the integer x is greater than the integer y.

x>y


Set pelajaran terkait

Chapter 5 and 6 checkpoint APESC

View Set

Sociology Chapter 4: Socialization, Interaction and the Self

View Set

Important Nobel Prize Laureates 1901-1960

View Set

Agency Law: Insurance Applications

View Set

Comp Final Project (Viktoria Genova)

View Set

COMP10002 Foundations of Algorithms

View Set