Docsity

Quiz 3 with Solution - Java Programming | COP 2250, Quizzes of Javascript programming

Toggle sidebar
bg1
Quiz 3
Name: Panther ID:
1. Add the following lines to the main method:
String s1 = new String(“\tWorld!\n”);
String s2 = “Hello”;
s3 = s1;
System.out.println(“s3: ” + s3);
s1 = s2 + s1;
System.out.println(“s3: ” + s3);
System.out.println(“Length of s1: ” + s1.length());
System.out.println(“Substring: ” + s1.substring(3, 6));
[1 pts] Is this program correct? If not, please correct it.
Not correct! Variable s3 should be declared as:
String s3 = s1;
[4 pts] What is program output?
s3: World!
s3: World!
Length of s1: 13
Substring: lo
2. [5 pts] Please complete the following program, which contains only main method
and asks user to create random numbers as follows.
num1 – any number of 1, 2, 3, 4, 5
num2 – any number of -4, -2, 0, …, 10, 12
import java.util.Random;
public class RandomGenerator {
public static void main(String[] args) {
Random generator = new Random();
int randomNumber;
randomeNumber = 1+generator.nextInt(5);
System.out.println( "num1 is " + randomeNumber);
randomeNumber = 2 * generator.nextInt(9) - 4;
System.out.println( "num2 is " + randomeNumber);
}
}
[Bonus]
[1 pt] How to generate a random number which could be any number of -5, -2, 1,
4, …, 31?
randomeNumber = 3 * generator.nextInt(13) - 5;
[1 pt] What is the output of the following statement?
String s = 5+3*2+“Quiz 3”+5+3/5;
System.out.println(s.substring(1,4) + s.substring(8, 10));
1Qu50
pf2

Partial preview of the text

Download Quiz 3 with Solution - Java Programming | COP 2250 and more Quizzes Javascript programming in PDF only on Docsity!

Quiz 3

Name: Panther ID:

1. Add the following lines to the main method:

String s1 = new String(“\tWorld!\n”); String s2 = “Hello”; s3 = s1; System.out.println(“s3: ” + s3); s1 = s2 + s1; System.out.println(“s3: ” + s3); System.out.println(“Length of s1: ” + s1.length()); System.out.println(“Substring: ” + s1.substring(3, 6));

[1 pts] Is this program correct? If not, please correct it.

Not correct! Variable s3 should be declared as: String s3 = s1;

[4 pts] What is program output?

s3: World! s3: World! Length of s1: 13 Substring: lo

2. [5 pts] Please complete the following program, which contains only main method

and asks user to create random numbers as follows.

num1 – any number of 1, 2, 3, 4, 5

num2 – any number of -4, -2, 0, …, 10, 12

import java.util.Random;

public class RandomGenerator {

public static void main(String[] args) {

Random generator = new Random(); int randomNumber; randomeNumber = 1+generator.nextInt(5); System.out.println( "num1 is " + randomeNumber); randomeNumber = 2 * generator.nextInt(9) - 4; System.out.println( "num2 is " + randomeNumber);

[Bonus]

[1 pt] How to generate a random number which could be any number of -5, -2, 1,

randomeNumber = 3 * generator.nextInt(13) - 5;

[1 pt] What is the output of the following statement?

String s = 5+3*2+“Quiz 3”+5+3/5;

System.out.println(s.substring(1,4) + s.substring(8, 10));

1Qu

  1. [5 pts] Please complete the following program, which contains only main method and asks user to input temperature in Fahrenheit scale and output the temperature in Celsius scale. The arithmetic conversion equation is T(Celsius) = (5 / 9)* (T(Fahrenheit) – 32 ) For example, if the program asks user to input and the user types 88.8 on keyboard, the program should output “Temperature conversion: 88.8 F = 31.5 C”. Solution 1: import javax.swing.JOptionPane; public class Fahrenheit2Celsius { public static void main(String[] args) { double Fahrenheit = Double.parseDouble( JOptionPane.showInputDialog( null, "Enter temperature in Fahrenheit scale " ) ); double Celsius = (5.0 / 9.0)* (Fahrenheit – 32 ) ; JOptionPane.showMessageDialog( null, "Your temperature in Celsius is "+ Celsius ); } } Solution 2: import java.util.Scanner; public class DialogBoxDemo1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter temperature in Fahrenheit scale

"); double fahrenheit = scan.nextDouble( ); double celsius = (5.0/9.0)*(fahrenheit-32) ; System.out.println("Your temperature in Celsius is " + celsius); } }