Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write all answers on the paper provided. You do not have to do the problems in o

ID: 3575413 • Letter: W

Question

write all answers on the paper provided. You do not have to do the problems in order, but you must LABEL each problem with the correct problem number. PRINT all code LEGIBLY. X write an assignment statement to compute the gas mileage a car where the int values of miles traveled and gallons needed have already been input. The variable gas mileage needs to be declared and should be a double. V2. Rewrite the following program with proper indentation and formal make it easier to read. public My Program public static void main( Stringl 1 args) System out println( "Wow, this is messed up! 3. Write a set of instructions to prompt theuser for an int value and input it using the Scanner class into the variable x and prompt the user for a float value and input it using the Scanner class into the variable y. upper using three the name as enteredby the user:3) prints the name in all prints the initials of the user. V Write a program reads values representing hours, minutes, and seconds, and prints the total number

Explanation / Answer

Page 1

Questioin 1)

import java.util.*;
//Class Mileage
public class Mileage
{
   public static void main(String ss[])
   {
       //Scanner class object to accept data
       Scanner sc = new Scanner(System.in);
       double gas_mileage, miles_traveled, gallons_needed;
       //Accept data
       System.out.println("Enter Miles Traveled: ");
       miles_traveled = sc.nextDouble();
       System.out.println("Enter Gallons Needed: ");
       gallons_needed = sc.nextDouble();
       //Calculates the gas mileage
       gas_mileage = miles_traveled / gallons_needed;
       System.out.println("Millage = " + gas_mileage);      
   }
}

Output:

Enter Miles Traveled:
1000
Enter Gallons Needed:
10
Millage = 100.0

Question 2)

public class MyProgram
{
   public static void main(String [] args)
   {
       System.out.println("Wow, this is messed up!");
   }
}

Output:

Wow, this is messed up!

Question 3)

import java.util.*;
//Class defined
public class MyProgram
{
   public static void main(String [] args)
   {
       //Declaration of variable
       int x;
       float y;
       //Scanner class object created to accept data
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a integer value");
       //Accepts an integer data
       x = sc.nextInt();
       System.out.println("Enter a float value");
       //Accepts an float data
       y = sc.nextFloat();
       System.out.println("Integer value = " + x + " Float value = " + y);
   }
}

Output:

Enter a integer value
20
Enter a float value
2.3
Integer value = 20 Float value = 2.3

Question 4)

import java.util.*;
//Class defined
public class StringManipulation
{
   public static void main(String [] args)
   {
       //Declaration of variable
       String firstName, middleName, lastName;
       //Scanner class object created to accept data
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter First Name: ");
       //Accepts first name
       firstName = sc.next();
       System.out.println("Enter Middle Name: ");
       //Accepts middle name
       middleName = sc.next();
       System.out.println("Enter Last Name: ");
       //Accepts last name
       lastName = sc.next();
       //Display the first, middle and last name
       System.out.println("First Name = " + firstName + " Middle Name = " + middleName + " Last Name = " + lastName);
       //Display the first, middle and last name in upper case
       System.out.println("First Name = " + firstName.toUpperCase() + " Middle Name = " + middleName.toUpperCase() + " Last Name = " + lastName.toUpperCase());
       //Display the first, middle and last name initials
       System.out.println("First Name = " + firstName.charAt(0) + " Middle Name = " + middleName.charAt(0) + " Last Name = " + lastName.charAt(0));
   }
}

Output:

Enter First Name:
Pyari
Enter Middle Name:
Mohan
Enter Last Name:
Sahu
First Name = Pyari
Middle Name = Mohan
Last Name = Sahu
First Name = PYARI
Middle Name = MOHAN
Last Name = SAHU
First Name = P
Middle Name = M
Last Name = S

Question 5)

import java.util.*;
//Class defined
public class Time
{
   public static void main(String [] args)
   {
       //Declaration of variable
       int hour, min, sec, res;
       //Scanner class object created to accept data
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter Hour: ");
       //Accepts Hours
       hour = sc.nextInt();
       System.out.println("Enter Minutes: ");
       //Accepts Minutes
       min = sc.nextInt();
       System.out.println("Enter Last Name: ");
       //Accepts Second
       sec = sc.nextInt();
       res = sec + (min * 60) + (hour * 60 * 60);
       //Display the hours, minutes seconds with total in second
       System.out.println("Hour = " + hour + " Minutes = " + min + " Seconds = " + sec + " Total number of Seconds: " + res);
   }
}

Output:

Enter Hour:
1
Enter Minutes:
2
Enter Last Name:
30
Hour = 1
Minutes = 2
Seconds = 30
Total number of Seconds: 3750

Page 2

Question 1)

import java.util.*;
//Class Number defination
class Number
{
   public static void main(String ss[])
   {
       int no, co = 0;
       char ch;
       float sum = 0;
       //Scanner class object created to accept data
       Scanner sc = new Scanner(System.in);
       //Loops till user enters 'n' or 'N'
       do
       {
           //Accepts user choice
           System.out.println("Would you like to Continue? (y/n)");
           //Extracts statring position
           ch = sc.next().charAt(0);
           //If choice is 'y' or 'Y'
           if(ch == 'y' || ch == 'Y')
           {
               System.out.println("Enter a number: ");
               //Accept number
               no = sc.nextInt();
               //Calculate sum
               sum += no;
               //Counter incresed to count number of numbers entered
               co++;
           }
           //If choice is 'N' or 'n' stop entering data
           else if(ch == 'N' || ch == 'n')
               break;
           else
               System.out.println("Wrong Choice! ");
       }while(true);
       //If sum is zero means no number is entered
       if(sum == 0)
           System.out.println("No value entered");
       //Otherwise display the average
       else
           System.out.println("Average = " + (sum / co));
   }
}

Output:

Would you like to Continue? (y/n)
y
Enter a number:
23
Would you like to Continue? (y/n)
y
Enter a number:
25
Would you like to Continue? (y/n)
y
Enter a number:
13
Would you like to Continue? (y/n)
n
Average = 20.333334

Question 2)

import java.util.*;
//Class Range defination
class Range
{
   public static void main(String ss[])
   {
       int no;
       //Scanner class object created to accept data
       Scanner sc = new Scanner(System.in);
       //Loops till user enters number within range 0 and 100
       do
       {
           System.out.println("Enter a number: ");
           //Accept number
           no = sc.nextInt();
           //Checks for the invalid range
           if(no < 0 || no > 100)
               break;
       }while(true);
   }
}

Output:

Enter a number:
55
Enter a number:
99
Enter a number:
78
Enter a number:
5
Enter a number:
-1

Question 3)

import java.util.*;
//Class Student definition
class Student
{
   //Data member
   String name;
   double test1, test2;
   //Parameterized constructor to initialize data members
   Student(String n, double t1, double t2)
   {
       name = n;
       test1 = t1;
       test2 = t2;
   }
   //Returns the name of the student
   String getName()
   {
       return name;
   }
   //Calculates the average and returns it
   double getAverage()
   {
       return (test1 + test2) / 2;
   }
   //Accepts the grade
   void inputGrades()
   {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter student grade: ");
       char g = sc.next().charAt(0);
   }
   //Displays the student name and average mark
   public String toString()
   {
       String msg = "";
       msg = " Student Name: " + getName() + " Average Mark: " + String.valueOf(getAverage());
       return msg;
   }
}
class Grades
{
   public static void main(String st[])
   {
       //Creates an object of student with information by calling parameterized constructor
       Student ss = new Student("Pyari", 90.22, 80.22);
       //Implicitly calls the toString() method to display student information
       System.out.println(ss);
   }
}

Output:

Student Name: Pyari
Average Mark: 85.22

Question 4)

import java.util.*;
//Class Count Space Vowel defination
class CountSpaceVowel
{
   public static void main(String ss[])
   {
       String data;
       //Scanner class object to accept data
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a sentence: ");
       //Accept a string
       data = sc.nextLine();
       //Counter for each vowel and space is initialize to zero
       int ca = 0, ce = 0, ci = 0, co = 0, cu = 0, cs = 0;
       //Loops till end of the string
       for(int c = 0; c < data.length(); c++)
       {
           //Extracts a character from the string
           char ch = data.charAt(c);
           //Pas the character to switch for checking
           switch(ch)
           {
               //If vowel a
               case 'a': case 'A':
                   //Increase the counter for a
                   ca++;
               break;
               //If vowel e
               case 'e': case 'E':
               //Increase the counter for e
                   ce++;
               break;
               //If vowel i
               case 'i': case 'I':
               //Increase the counter for i
                   ci++;
               break;
               //If vowel o
               case 'o': case 'O':
               //Increase the counter for o
                   co++;
               break;
               //If vowel u
               case 'u': case 'U':
               //Increase the counter for u
                   ce++;
               break;
               //If space
               case ' ':
                   cs++;
               break;
           }
       }
       //Displays counter for each vowel and space
       System.out.println("Number of a = " + ca + " Number of e = " + ce + " Number of i = " + ci + " Number of o = " + co + " Number of u = " + cu + " Number of Space = " + cs);
   }
}

Output:

Enter a sentence:
This Is A Demo
Number of a = 1
Number of e = 1
Number of i = 2
Number of o = 1
Number of u = 0
Number of Space = 3