Example sheet 1
What is the output produced by the following lines of Java code?
int quotient, remainder;
quotient = 7/3;
Copyright By PowCoder代写 加微信 powcoder
remainder = 7%3;
System.out.println(”quotient = “ + quotient); System.out.println(”remainder = “ + remainder);
What is the output produced by the following code?
int result = 11;
result /=2; // this is the same as result = result/2
System.out.println(”result is “ + result); 2.
Given the following fragment that is meant to convert from degrees Celsius to degrees Fahrenheit, answer the questions below:
double celsius = 20; double fahrenheit;
fahrenheit = (9/5)*celsius + 32.0;
(a) What value is assigned to fahrenheit?
(b) Explain what is happening, and what the programmer likely wanted.
(c) Rewrite the code as the programmer intended.
What are the values assigned to a, b and c?
int a = 5*4+11-6/3+2; int b = 5*(4+11-6)/3+2;
int c = 5*(4+11)-6/(3+2); 5.
What is the output from the following?
int x = 15;
double y = 4.0;
double z = x / y; System.out.println(”z is “ + z); x = 15;
x /= 4.0;
System.out.println(”x is “ + x);
What is the final value of wholeString?
string1 = ”have discussed”; string2 = ”the relative”; string3 = ”to IoT technology”; string4 = ”Many researchers”; string5 = ”privacy risks”; space = ” “;
wholeString = string4 + space + string1 + space + string2 + space + string5 + space + string3;
Suppose that my class has the following method declaration:
public static double interest(double deposit, double rate){ double myInterest = deposit * rate/100;
return myInterest;
and my main method contains the following code:
double rate = 15;
double myBalance = 200; double yourBalance = 150;
System.out.println(”The interest I have earned is “ + interest(myBalance, rate));
System.out.println(”The interest you have earned is “ + interest(yourBalance, rate));
a. What is the output?
b. If I included the following line of code inside my main method (under the two println
statements above), there is an error. Why?
System.out.println(”The interest I have earned is “ + myInterest);
c. Suppose I included another method in my class, as follows:
public static double interest(double deposit, double rate, int cap){ double myInterest = deposit * rate/100;
if(myInterest<cap) return myInterest;
else return cap;
And the following statements to my main method:
System.out.println("The capped interest I have earned is " + interest(myBalance, rate, 25));
System.out.println("The capped interest you have earned is " + interest(yourBalance, rate, 25));
What would the new output be? 8.
I want some code that, if I input the string “Agnes”, produces the output:
Agnes has submitted a request: Request validated Suppose that my class has the following two methods:
public static String validate(){
return "Request validated"; }
public static void answer(String name){
System.out.print("\n" + name + " has submitted a request: "); validate();
And my main method contains the following code: answer("Agnes");
Do I get the output I want? If not, what have I done wrong?
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com