JAva Code Guide book Chp 3.

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Write an expression that returns a random integers between 0 and 9, inclusive.

(int)(Math.random() * 10)

Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true. A. if (isPrime = true) B. if (isPrime == true) C. if (isPrime) D. if (!isPrime = false) E. if (!isPrime == false)

B. if (isPrime == true) C. if (isPrime) E. if (!isPrime == false)

What is the output from System.out.println((int)Math.random() * 4)?

Casting is performed before the * operator in (int)Math.random() * 4. So, it returns 0

Assume the availability of class named DateManager that provides a static method, printTodaysDate, that accepts no arguments and returns no value. Write a statement that calls printTodaysDate.

DateManager.printTodaysDate();

Declare a reference variable of type File named myFile.

File myFile;

develop a program to play lottery. The program randomly generates a lottery of a two-digit number, prompts the user to enter a two-digit number

Generate a lottery int lottery = (int)(Math.random() * 100); // Prompt the user to enter a guess Scanner input = new Scanner(System.in); System.out.print("Enter your lottery pick (two digits): "); int guess = input.nextInt(); // Get digits from lottery int lotteryDigit1 = lottery / 10; int lotteryDigit2 = lottery % 10; // Get digits from guess int guessDigit1 = guess / 10; int guessDigit2 = guess % 10;

Declare a variable named myMenu suitable for holding references to Menu objects.

Menu myMenu ;

write a program that prompts the user to enter a year and displays the animal for the year. ( using a switch).

Scanner input = new Scanner(System.in); 6 7 System.out.print("Enter a year: "); 8 int year = input.nextInt(); 9 10 switch (year % 12) { 11 case 0: System.out.println("monkey"); break; 12 case 1: System.out.println("rooster"); break; 13 case 2: System.out.println("dog"); break; 14 case 3: System.out.println("pig"); break; 15 case 4: System.out.println("rat"); break; 16 case 5: System.out.println("ox"); break; 17 case 6: System.out.println("tiger"); break; 18 case 7: System.out.println("rabbit"); break; 19 case 8: System.out.println("dragon"); break; 20 case 9: System.out.println("snake"); break; 21 case 10: System.out.println("horse"); break; 22 case 11: System.out.println("sheep"); break; 23 } 24 } 25 }

write the code that will Prompt the student to answer "what is number1 − number2?"

System.out.print ("What is " + number1 + " - " + number2 + "? "); Scanner input = new Scanner(System.in); int answer = input.nextInt();

write a program that checks whether a number is divisible by 2 and 3, by 2 or 3, and by 2 or 3 but not both:

System.out.print("Enter an integer: "); int number = input.nextInt(); if (number % 2 == 0 && number % 3 == 0) System.out.println(number + " is divisible by 2 and 3."); if (number % 2 == 0 || number % 3 == 0) System.out.println(number + " is divisible by 2 or 3."); if (number % 2 == 0 ^ number % 3 == 0) System.out.println(number + " divisible by 2 or 3, but not both."); } }

write code to Prompt the user to enter weight in pounds and to enter height in inches. then set kg and inches into a variable.

System.out.print("Enter weight in pounds: "); double weight = input.nextDouble(); System.out.print("Enter height in inches: "); double height = input.nextDouble(); final double KILOGRAMS_PER_POUND = 0.45359237; final double METERS_PER_INCH = 0.0254;

Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even"); }

The program runs fine and displays It is even. because It is a common mistake to use the = operator in the condition test. What happens is that true is assigned to even when you write even = true. So even is true. The program compiles and runs fine and displays 'It is even'.

make current code concise. if (number % 2 == 0) even = true; else even = false;

boolean even = number % 2 ==0;

Write the definitions of two classes Day and Night. Both classes have no constructors, methods or instance variables. Note: For this exercise, please do not declare your classes using the public visibility modifier.

class Day{} class Night {}

write the code to calculate the expression " 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 " with a float number.

final double EPSILON = 1E-14; double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1; if (Math.abs(x - 0.5) < EPSILON) System.out.println(x + " is approximately 0.5");

write the example of a two way "if " statement.

if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

Write an one way " if" statement.

if (boolean-expression) { statement(s); }

write an example of an nested "if" statement.

if (i > k) { if (j > k) System.out.println("i and j are greater than k"); } else System.out.println("i is less than or equal to k");

using the "if-else" statement checks whether a number is even or odd, as follows:

if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd.");

If number1 < number2, swap number1 with number2

if (number1 < number2) { int temp = number1; number1 = number2; number2 = temp; }

write a code using "if " statement to compute area.

if (radius >= 0) { area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + " is " + area); }

write a code to prints a letter grade according to the score, with multiple alternatives.

if (score >= 90) System.out.print("A"); else if (score >= 80) System.out.print("B"); else if (score >= 70) System.out.print("C"); else if (score >= 60) System.out.print("D"); else System.out.print("F");

translate the code

if (x > 0) y = 1; else y = -1;

Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note that one pound is 0.45359237 kilograms and one inch is 0.0254 meters. Listing 3.4 gives the program.

import java.util.Scanner; public class ComputeAndInterpretBMI { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight =input.nextDouble(); // Prompt the user to enter height in inches System.out.print("Enter height in inches: "); double height = input.nextDouble(); final double KILOGRAMS_PER_POUND = 0.45359237; // Constant final double METERS_PER_INCH = 0.0254; // Constant // Compute BMI double weightInKilograms = weight * KILOGRAMS_PER_POUND; double heightInMeters = height * METERS_PER_INCH; double bmi = weightInKilograms / (heightInMeters * heightInMeters); // Display result System.out.println("BMI is " + bmi); if (bmi < 18.5) System.out.println("Underweight"); else if (bmi < 25) System.out.println("Normal"); else if (bmi < 30) System.out.println("Overweight"); else System.out.println("Obese"); } }

write the code to set and use the Scanner.

import java.util.Scanner; Scanner input = new Scanner(System.in); int answer = input.nextInt();

a program that prompts the user to enter an integer. If the number is a multiple of 5, the program displays HiFive. If the number is divisible by 2, it displays HiEven.

import java.util.Scanner; 2 3 public class SimpleIfDemo { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.print("Enter an integer: "); 7 int number = input.nextInt(); 8 9 if (number % 5 == 0) 10 System.out.println("HiFive"); 11 12 if (number % 2 == 0) 13 System.out.println("HiEven"); 14 } 15 }

write a program to compute personal income tax. Your program should prompt the user to enter the filing status and taxable income and compute the tax. Enter 0 for single filers, 1 for married filing jointly or qualified widow(er), 2 for married filing separately, and 3 for head of household.

import java.util.Scanner; 2 3 public class ComputeTax { 4 public static void main(String[] args) { 5 // Create a Scanner 6 Scanner input = new Scanner(System.in); 7 8 // Prompt the user to enter filing status 9 System.out.print("(0-single filer, 1-married jointly or " + 10 "qualifying widow(er), 2-married separately, 3-head of " + 11 "household) Enter the filing status: "); 12 13 int status = input.nextInt(); 14 15 // Prompt the user to enter taxable income 16 System.out.print("Enter the taxable income: "); 17 double income = input.nextDouble(); 18 19 // Compute tax 20 double tax = 0; 21 22 if (status == 0) { // Compute tax for single filers 23 if (income <= 8350) 24 tax = income * 0.10; 25 else if (income <= 33950) 26 tax = 8350 * 0.10 + (income - 8350) * 0.15; 27 else if (income <= 82250) 28 tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 29 (income - 33950) * 0.25; 30 else if (income <= 171550) 31 tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 32 (82250 - 33950) * 0.25 + (income - 82250) * 0.28; 33 else if (income <= 372950) 34 tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 35 (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + 36 (income - 171550) * 0.33; 37 else 38 tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 39 (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + 40 (372950 - 171550) * 0.33 + (income - 372950) * 0.35; 41 }

write the code to set 1, 2 and 3 to a variable.

int i = 1, j = 2, k = 3;

write the code to " Generate two random single-digit integers"

int number1 = (int)(Math.random() * 10); int number2 = (int)(Math.random() * 10);

The following code displays ___________. Question: double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right");

just right

Assume that logger is a reference to an object that has a method named printLarger that accepts two int arguments and returns no value. Two int variables, sales1 and sales2, have already been declared and initialized. Write a statement that calls printLarger, passing it sales1 and sales2.

logger.printLarger(sales1, sales2);

Fix the following code..... if (radius >= 0) area = radius * radius * PI; System.out.println("The area " + " is " + area);

missing braces if (radius >= 0) { area = radius * radius * PI; System.out.println("The area " + " is " + area); }

identify what can make code more concise. if (inState) { tuition = 5000; System.out.println("The tuition is " + tuition); } else { tuition = 15000; System.out.println("The tuition is " + tuition); }

print statement. if (inState) { tuition = 5000; } else { tuition = 15000; } System.out.println("The tuition is " + tuition);

A class named Clock has two instance variables: hours (type int) and isTicking (type boolean). Write a constructor that takes a reference to an existing Clock object as a parameter and copies that object's instance variables to the object being created. Your task: Write a Clock constructor that makes this possible. Just write this constructor—don't write the whole class or any code outside of the class!

public Clock (Clock c) { hours = c.hours; isTicking = c.isTicking; }

You are given a class named Clock that has three instance variables: One of type int called hours, another of type boolean called isTicking, and the last one of type int called diff. Write a constructor for the class Clock that takes three parameters—an int, a boolean, and another int. The constructor should set the instance variables to the values provided.

public Clock (int hours,boolean isTicking, int diff) { this.hours = hours; this.isTicking = isTicking; this.diff = new Integer (diff); }

You are given a class named Clock that has one int instance variable called hours. Write a constructor with no parameters for the class Clock. The constructor should set hours to 12.

public Clock() { hours = 12; }

Write a public class named Acc1 containing no constructors, methods, or instance variables. (Note, the last character of the classname is "one" not "ell".)

public class Acc1 { }

Write the definition of a public class Simple. The class has no constructors, methods or instance variables.

public class Simple { }

The Math.abs(a) method can be used to

return the absolute value of a.

Translate the following code: "26 else if (guessDigit2 == lotteryDigit1 27 && guessDigit1 == lotteryDigit2)"

this is saying if the guesses are equal to the opposite sequence.

The and (& &) of two Boolean operands is

true if and only if both operands are true. (age > 28) && (weight <= 140) is false, because (age > 28) is false.

exclusive or (^) of two Boolean operands is

true if and only if the two operands have different Boolean values.

Assume x is an integer. Write a simple expression that returns true if x is even or false if x is odd.

x % 2 == 0

Write a simple expression that returns true if x is even or false if x is odd.

x % 2 == 0

Suppose x = 1, y = -1, and z = 1. What is the output of the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");

x < 0 and z > 0;


संबंधित स्टडी सेट्स

PEDS Cancer and end of life questions

View Set

Lesson One: Origins of Medical Terminology

View Set

Anth 026 FINAL exam blackboard questions

View Set

Intermediate Accounting I C248 Chapter 4 & 5

View Set

The Science and Engineering of Materials [Revised]

View Set