In: Computer Science
JAVA PROGRAM
(Make sure that programs are running. Please discuss, if there is any compilation or run error.)
Q. 1 Calculate the expression
a) x= 7.0 + (12 %3)*5 – 3;
b) x= 7 + (11 / 2)*5 + 3;
Q 2: Write a program that prompts the user to enter five test scores and then prints
the average test score. (Assume that the test scores are decimal numbers.)
Q 3. Write a program that prompts the capacity, in gallons, of an automobile fuel tank and the miles per gallon the automobile can be driven. The program outputs the number of miles the automobile can be driven
without refueling.
Q 4. Centigrade and Fahrenheit are two scales to measure temperature. Write a program that that prompts the user to enter Centigrate and then print the Fahrenheit.
Use following formula for conversion.
Fahrenheit = Centigrade*1.8 + 32 ;
Q. 5 What is the output of the following program?
public class SwitchDemo {
public static void main(String[] args) {
int x = 2;
switch (x)
{ case 1: cout << "x is 1";
case 2: cout << "x is 2";
default: cout << "value of x unknown"; } }
Q. 6
a) Which of the following are valid java identifiers (variables)?
• 1MyFirstProgram
• Mix_up
• Fifth.x
• five
b) Write whethet the following expressions are true or false
I. 8>8&&9>8
II. 8>8| |8>9
Q. 7 Write a program to calculate the bill of a customer. The program will
- Prompt the employee to enter the monthly plan fees.
- Prompt the employee to enter the rate per additional minute.
- Print the bill
The bill will be calculated as follows: if the number of consumed minutes is 300 minutes or less, the client pays only his regular monthly fees. If the number of minutes is more than 300, then he should pay in addition to the regular monthly fees, an extra amount for each additional minute consumed (over the allowed 300 minutes using rate per additional minute.).
Answering only the ans of highlighted Questions.
ANS 2)
CODE:
import java.io.*;
import java.util.*;
class Test {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter five test scores");
double sum=0.0;
for(int i=0;i<5;i++)
{
sum+=sc.nextDouble();
}
System.out.print("The avg test score is "+(double)(sum/5));
}
}
OUTPUT:
ANS 3)
CODE:
import java.io.*;
import java.util.*;
class Test {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
double capc;
double mp_gallon;
System.out.println("Enter the capacity in gallons :");
capc=sc.nextDouble();
System.out.println("Enter miles per gallons :");
mp_gallon=sc.nextDouble();
double totalmiles;
totalmiles = (double)capc*mp_gallon;
System.out.println("The no. of miles the vehicle can drive without
refueling : "+String.format("%.2f",totalmiles));
}
}
OUTPUT:
ANS 5)
Your code is a mixture of JAVA AND C++.
Please use any one language for executing the code.
The Output of this Code is Syntax Error.(THERE IS NO cout<< STATEMENT IN JAVA ).
There is also no proper indexing of brackets, the compiler will reach the end of file while parsing. Include a '}' at the end of your code for successful execution.
Howerver, considering that this code is fully written in JAVA with no errors.
THE OUPUT WILL BE:
x is 2 value of x unknown
Explanation: Since there is no break statement between the cases. When case 2 is satisfied, all the cases till the end of the switch statement will execute.
ANS 6a):
Mix_up and five are valid java identifiers .
1MyFirstProgram is not valid because Identifiers should not start with digits([0-9]).
Fifth.x is not valid because the only valid characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘ and ‘_‘ .
ANS 6b):
Both the expressions gives false as output.
First expression: 8>8&&9>8 includes an and operator which only gives true if both the conditions are true. But here in this case only condition is true.
Second expression: 8>8| |8>9 includes an or operator which gives true even if one conditions is true. But here in this case both conditions are false.