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

3.1 Write an Algorithm to decide which is cheaper car or bus for a travel: You w

ID: 642579 • Letter: 3

Question

3.1 Write an Algorithm to decide which is cheaper car or bus for a travel:

You want to decide whether you should drive your car to work or take the bus. You know the one-way distance from your home to your place of work, and the fuel efficiency of your car (in miles per gallon). You also know the one-way price of a train ticket. You assume the cost of gas at $4 per gallon, and car maintenance at 5 cents per mile. Write an algorithm to decide which commute is cheaper.

3.2 Use string concatenation only to complete the following program so that it prints the message "the quick white tiger jumps over the small dog".

public class ConcatDemo

{

public static void main(String[] args)

{

String animal1 = "quick white tiger"; String animal2 = "small dog";

String article = "the"; String action = "jumps over";

/* Your work goes here */

System.out.println(message);

}

}

3.3 Find the output of the following code when the user enters 100 and when the user enters AQ:

public static void main(String[] args)

{

Scanner in = new Scanner(System.in); System.out.print("Enter somthing: "); String str = in.next();

int count = Integer.parseInt(str); System.out.println("Input is " + count);

}

Explanation / Answer

3.1)

//Algorthem.java
public class Algorthem
{
   public static void main(String[] args)
   {
      
       //variable to store car fuel and car maitainance charges
       double carFuelConsumption=4;
       double carMaintainance=5;
      
       //variables to store distance by car
       double distanceByCar=10;
       //variable to store train charge
       double trainTicketCharge=10;
      
       //calculate cost of car journey
       double costOfCarJourney=
               (distanceByCar*carFuelConsumption)
               +(distanceByCar*carMaintainance);
      
       //check if the car journety cost is greater than train journey
       if(costOfCarJourney>trainTicketCharge)
           System.out.println("Better take Train journey");
       else
           System.out.println("Better take Car journey");
      
      
      
      
   }
}

Sample output:

Better take Train journey

---------------------------------------------------------------------------------

3.2)


//CreateDemo.java
public class CreateDemo
{
   public static void main(String[] args)
   {
       String animal1 = "quick white tiger";
       String animal2 = "small dog";
       String article = "the";
       String action = "jumps over";
       //concatenate strings concat function
       System.out.println(article.concat(" "+animal1).concat(" "+action).concat(" "+animal2));

   }

}

Sample output:

the quick white tiger jumps over small dog

---------------------------------------------------------------------------------

3.3)


//Output.java
import java.util.Scanner;
public class Output
{
   public static void main(String[] args)
   {
       Scanner in = new Scanner(System.in);
       System.out.print("Enter somthing: ");
       String str = in.next();
       int count = Integer.parseInt(str);
       System.out.println("Input is " + count);

   }
}


Sample output:

Enter somthing: 100
Input is 100

Sample run2:

Enter somthing: AQ
Number format Exception occurs

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote