Showing posts with label Java Programming. Show all posts
Showing posts with label Java Programming. Show all posts

Monday, October 25, 2021

A program that determines whether an input number is an integer or a real number in java

 write a program that determines whether an input number is an integer or a real number.

Three sample runs of this program are as follows:

Enter a number: 50

The number 50 is an integer.

Enter a number: 50.0

The number 50 is an integer.

Enter a number: 50.75

The number 50.75 is a real number.

HINTS: Use only ONE variable for the input. You will also need to separate the real part (after the decimal point) of the input and the integer part (before the decimal point). Think about how you will do this.


Code:

public class NumberTypeLab5{
    public static void main(String[] args) {
     int x =25;
     x = x*100;
     if(x/100==0){
      System.out.println("Number is not integer: " + x);
     }  
     else{
      System.out.println("Number is integer: " + x);
     }
  }}

Program that calculates and displays a person’s body mass index (BMI) in java

write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person with an inactive lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:

BMI = Weight × 703 / Height2

where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. An inactive person’s weight is considered to display a message indicating whether the person has optimal weight, is underweight, or is overweight. An inactive person’s weight is considered optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered 18.5 and 25. If the BMI is less than 18.5, the person is considered underweight. If the BMI value is greater than 25, the person is considered value is greater than 25, the person is considered overweight


Code:


import java.util.Scanner;
   class BMILab5{
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double weight, height, bmi;
      System.out.println("Enter Weight Value: ");
      weight = input.nextDouble();
      System.out.println("Enter Height: ");
      height = input.nextDouble();
      bmi = weight + 703 / Math.pow(height, 2);
      System.out.println("BMI Is: " + bmi);
      if(bmi >= 18.5 && bmi <= 25){
      System.out.println("Weight is Optimal:" + bmi);
      }else if(bmi < 18.5){
      System.out.println("Weight is Underweight:" + bmi);
      }else if (bmi > 25){
      System.out.println("Weight is overweight: " + bmi);
      }}}

OutPut: 



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


Math Class Function In Java

 In this task, you are being asked to apply Math class functions.
Write a program that computes the first and last digit of a number. For example, if the input is
23456, the program should print 2 and 6.
Hint: Use % and Math.log10.

Code:

import java.util.Scanner;
public class Lab4Task4{
public static void main(String[] args) {
Scanner Ahsan = new Scanner(System.in);

int firstDigit, lastDigit, num;
System.out.print("Enter the number: ");
num = Ahsan.nextInt();

firstDigit = num / (int) Math.pow(10, (int) Math.log10(num));
lastDigit = num % 10;
System.out.println("Input number: " + num);
System.out.println("First Digit is: " + firstDigit);
System.out.println("Last Digit is: " + lastDigit);
}
}

OutPut:

Math Class Function In Java


Friday, October 22, 2021

Passing Array to Method and Average of Array Elements

 In this task, we complete the task Passing Array to Method and Average of Array Elements in java Using the Scanner.

Code:

import java.util.Scanner;
public class AvgArray{
  public static void main(String[] args){

       Scanner input = new Scanner(System.in);
       int size;

      // Size of Array
      System.out.print("Enter size of Array: ");
      System.out.println("\n\n");

      //Declaring and creating an array of specified size
      int[] numbers = new int[size];


      //Take Array elements from user
      for(int i=0; i<size; i++){
         System.out.print("Enter value for numbers[" + i + "] = ");
         numbers[i] = input.nextInt();
         System.out.println("\n\n");
      }

      // Calling method to find average of elements
      int avg = avgArray(numbers);  


      System.out.print("\nAverage of Array Elements: " + avg);  
   
  }//main

  public static int avgArray(int[] array){
      
      //Get the size of array
      int sum = 0, avg;

      //iterate over each element and print it
      for(int i = 0; i < array.length; ++i){
            sum = sum + array[i];
      }

     avg = sum / array.length;
    return avg;
  }
}//class

Sum Arrays And Passing Arrays

 In this task, we perform code of Sum of Arrays And Passing Arrays through java code.

Code:

import java.util.Scanner;
public class SumArray{
  public static void main(String[] args){

       Scanner input = new Scanner(System.in);
       int size;

      // Size of Array
      System.out.print("Enter size of Array: ");
      size = input.nextInt();
      System.out.println("\n\n");

      //Declaring and creating an array of specified size
      int[] numbers = new int[size];


      //Take Array elements from user
      for(int i=0; i<size; i++){
         System.out.print("Enter value for numbers[" + i + "] = ");
         numbers[i] = input.nextInt();
         System.out.println("\n\n");
      }

      // Calling method to get sum of elements
      int sum = sumArray(numbers);  


      System.out.print("\nSum of Array Elements: " + sum);  
   
  }//main

  public static int sumArray(int[] array){
      
      //Get the size of array
      int sum = 0;

      //iterate over each element and print it
      for(int i = 0; i < array.length; ++i){
            sum = sum + array[i];
      }

    return sum;
  }
}//class

Writing and evaluating expressions in Java

 In this task, you are being asked to understand the evaluation order of various operators.

What are the values of the following expressions, assuming that n is 17 and m is 18? First, solve

all expressions on paper, and then write and solve in Java.

Make sure that you truly understand why and how each expression gets solved and produces

the result.

a. n / 10 + n % 10
b. n % 2 + m % 2
c. (m + n) / 2
d. (m + n) / 2.0
e. (int) (0.5 * (m + n))
f. (int) Math.round(0.5 * (m + n))

Now, crate a program called ExpressionsLab4.java, and write output statements and print both

the expression and its value.

Code: 

public class Lab4Task3{

public static void main(String[] args) {

double a, b, c, d;

int e, f, m, n;


m = 18;

n = 17;


a = n / 10 + n % 10;

b = n % 2 + m % 2;

c = (m + n) / 2;

d = (m + n) / 2.0;

e = (int) (0.5 * (m + n));

f = (int) Math.round (0.5 * (m + n));


System.out.println(a);

System.out.println(b);

System.out.println(c);

System.out.println(d);

System.out.println(e);

System.out.println(f);

}}

Mathematical expressions in Java

 In this task, you are being asked to understand the conversion of mathematical expressions into Java code.

Mathematical expressions in Java


1. public class Lab4Task2{

public static void main(String[] args) {

double s, s0, v0, t, g;


s0 = 12;

v0 = 24;

t = 50;

g = 9.0;


s = s0 + v0 * t + 1/2 * g * Math.pow(t,2);

System.out.println(s);

}}


2. public class Lab4Tak2{

public static void main(String[] args) {

double FV, PV, YRS, INT;

PV = 5.5;

YRS = 2;

INT = 500;

FV = PV *Math.pow(1 + INT / 100 , YRS);

System.out.println(FV);

}}


3. public class Lab4Tsk2{

public static void main(String[] args) {

double G, a, p, m1, m2;

a = 4;

p = 6;

m1 = 7;

m2 = 9;

G = 4 * Math. pi   * Math.pow(a,3) / Math.pow(p,2) * (m1 + m2);

System.out.println(G);

}}

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