Showing posts with label Math Library in java. Show all posts
Showing posts with label Math Library in java. Show all posts

Saturday, October 23, 2021

Fuel Efficiency Through Java Code

 In this task, you are being asked to write a given word problem as a program.

Write a program that asks the user to input (using a Scanner object):

  •  The number of gallons of fuel in the tank
  •  The fuel efficiency in miles per gallon
  •  The price of fuel per gallon
Then print the cost per 100 miles and how far the car can go with the gas in the tank.

Code: 

import java.util.Scanner;
public class Lab4Task6{
public static void main(String[] args) {
Scanner Ahsan = new Scanner(System.in);
double noOfGalons, fuelEfficiency, price;
int hundredmile = 100;
System.out.print("Enter the no of Gallons of gas in tank: ");
noOfGalons = Ahsan.nextDouble();
System.out.print("Enter the Fuel Efficiency: ");
fuelEfficiency = Ahsan.nextDouble();
System.out.print("Enter the Price of Fuel per gallon: ");
price = Ahsan.nextDouble();
double CostPerHundredmile = (hundredmile / fuelEfficiency ) * price;
System.out.println("Cost of per 100 mile is: " + CostPerHundredmile);
double distance = noOfGalons * fuelEfficiency;
System.out.println("Total distance cover by car is: " + distance + "miles");
}

OutPut:

Convert word problem into Java code

 In this task, you are being asked to write a given word problem as a program.

Write a program that prompts the user for a radius and then prints:

  •  The area and circumference of a circle with that radius
  •  The volume and surface area of a sphere with that radius

HINT: Area = π×radius², Circumference=2×π×radius,
Volume of sphere = (4/3)×π×radius3, Surface area = 4×π×radius2.

NOTE: Use Math class constants and functions in your program.

Code:

import java.util.Scanner;

public class lab4Task5{

public static void main(String[] args) {

Scanner Ahsan = new Scanner(System.in);

double area, CircumFarance, volum, Surfac, radius; 

System.out.print("Enter the Radius: "); 

radius = Ahsan.nextDouble();


area = Math.PI * Math.pow(radius, 2);

CircumFarance = 2 * Math.PI * radius;

volum =(int) (4 / 3) * Math.PI * Math.pow(radius, 3);

Surfac = 4 * Math.PI * Math.pow(radius, 3);

System.out.println("Area of the Circle is:                " + area);

System.out.println("The CircumFarance of the Circle is:   " + CircumFarance);

System.out.println("The Volume of the Circle is:          "+ volum);

System.out.println("The SurfacArea of the Circle is:      " + Surfac);

}

}

OutPut:

Convert word problem into Java code


String Array and passing method in java

 In this task, we pass a method and also cover the passing String Array to the method. Code:   import java.util.Scanner; public class String...