Question

In: Computer Science

JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or...

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

Solutions

Expert Solution

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.


Related Solutions

Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch multiple methods testing In this document program logic & program structure (class, methods, variables) input / output assumptions and limitations documentation & style suggested schedule cover letter discussion questions grading, submission, and due date additional notes challenges sample output test plan Program Logic The program prompts the user to select a type of math problem (addition,...
a.) Please be sure to use the textbook (Macroeconomic) and any other websites to discuss Fiscal...
a.) Please be sure to use the textbook (Macroeconomic) and any other websites to discuss Fiscal Policy. Include in your paper the importance and significance of the Council of Economic Advisors. This is to be different than your assignment submission regarding current issues of Fiscal Policy. You are not to simply cut and paste information from the CEA Website. In this look at Fiscal Policy, I would like you to demonstrate your understanding of how the study of Fiscal Policy...
THIS IS EXAM REVIEW, so please make sure to show the work and make sure the...
THIS IS EXAM REVIEW, so please make sure to show the work and make sure the work is correct. In all tests of hypothesis use the 5% level of significance unless told otherwise. In all confidence interval problems use 95% confidence unless told otherwise. SHOW YOUR WORK. 2. Test scores for a mathematics course have a normal distribution with a mean of 76 and a standard deviation of 12. a.What proportion of scores will be between 70 and 90? b....
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will...
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will allow you to keep track of a set of employees. In reviewing your employee list, you notice that your employees fall into two categories: Salaried and Hourly. The following table shows the information that you keep in your employee list for each type of employee. Field Type Salaried Hourly id int Yes Yes name String Yes Yes title String Yes No position String No...
[JAVA] Please make sure there are FIVE (5) different source codes. Do not copy your answers...
[JAVA] Please make sure there are FIVE (5) different source codes. Do not copy your answers or someone’s else answers from different Cheggs. I did not post this question to see the same answers. All information that was given is included. Thank you. I will upvote if you do it properly. Task 1: Add three methods: union, intersection, and difference to the interface BagInterface for the ADT bag. Task 2: Implement the three methods, union, intersection, and difference, for the...
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
create a VHDL program for the Truth Table below. Please make sure to create your code...
create a VHDL program for the Truth Table below. Please make sure to create your code for the simplified circuit. A B C Z 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0
(Make sure in Mips!!!) Use the correct “syscall” to create MIPS programs for the following. !!!...
(Make sure in Mips!!!) Use the correct “syscall” to create MIPS programs for the following. !!! please use comments and ALWAYS use the correct syscall program code to end the program. !!! 1) Write a program in MIPS which asks the user to enter their favorite type of pie. The program should then print out "So you like _____ pie", where the blank line is replaced by the pie type entered. ALSO: What annoying feature of syscall service 4 makes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT