Monday, October 25, 2021

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 StringArray{
  public static void main(String[] args){

      String[] names = {"Babar", "Fakhar", "Kohli", "Gilchrist"};

      //Take Array elements from user
      for(int i=0; i<names.length; i++){
         System.out.print("\nnames[" + i + "] = " + names[i]);
      }
   
  }//main

}//class

OutPut:

Searching an Element in Array and Return true if found in java

 In this task pass the Array to a method and search an Element in Array and Return true if found in java. we use the Scanner method and search the element in Array.

Code:

import java.util.Scanner;
public class SearchingElement{
  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");
      }

      //Value to be searched
      System.out.print("Enter the Value you want to Search: ");
      int value = input.nextInt();

      // Calling method to find value
      boolean found = findValue(numbers,value);  
      if(found)
         System.out.print("\n " + value + " exists in the Array");  
      else
         System.out.println(value +" does not exist in Array");
   
  }//main

  public static boolean findValue(int[] array, int value){     
      //iterate over each element and print it
      for(int i = 0; i < array.length; ++i){
           if(array[i] == value){
                return true;
           } }
    return false;
  }
}//class

Code:

Searching an Element in Array and Return true if found in java

 

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: 



Grade Calculation in java

 In this task, you are being asked to write a code of the given problem in java. Write code for a program that assigns letter grades for a quiz, according to the following table:

Score

Grade

90-100

A

80-89

B

70-79

C

60-69

D

< 60

F


Code:

import java.util.Scanner;

class GradeCalculationLab5{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int marks;
System.out.println("Enter Your Marks: ");
marks = input.nextInt();
if(marks>=90 && marks<=100){
System.out.println("Grade Is:" + "A");

}else if(marks>=80 && marks<=89){
System.out.println("Grade Is: " + "B");
}else if(marks>=70 && marks<=79){
System.out.println("Grade Is: " + "C");

}else if(marks>=60 && marks<=69){
System.out.println("Grade Is: " + "D");

}else if (marks<60){
System.out.println("Fale:");
}}}

OutPut: 



Saturday, October 23, 2021

Create Flex box having a flexibility and make a Pop up when curser on it Through HTML and CSS

 In this task, we make a flexbox having flexibility and make a Pop up when the cursor is on it Through HTML and CSS. when the cursor is on the picture it gives a pop-up WHich time to your set on it.

Like Input:



Code:

<!DOCTYPE html>
<html>
<head>
<title>FlexBox</title>
</head>
<body>
<style>
#flex-container{
display: flex;
flex-flow: row wrap;
justify-content: center;
}
img:hover{
transform: scale(1.2);
transition: all 2s;
}
.flex-item{
border: 2px solid black;
margin: 15px;
}
h1{
border-bottom: solid;
width: 180px;
}
</style>
<h1>Wild Life</h1>
<div id="flex-container">
<div class="flex-item" ><img src="lion.jpg" style="height: 200px;width: 350px;"></div>
<div class="flex-item" ><img src="panda.jpg"  style="height: 200px;width: 350px;"></div>
<div class="flex-item"  ><img src="cheetah.jpg" style="height: 200px;width: 350px;"></div>
<div class="flex-item"  ><img src="eleph.png" style="height: 200px;width: 350px;"></div>
<div class="flex-item"  ><img src="markhor.jpg" style="height: 200px;width: 350px;"></div>
<div class="flex-item"  ><img src="zebra.jpg" style="height: 200px;width: 350px;"></div><br>
<button><a href="WildLife.htnl"></a>WildLife</button>
</div>
</body>
</html>

Finding Smallest Element in Array And Passing Array to Method in Java

 In this code, we cover the Finding Smallest Element in Array And Passing Array to Method through Java. we cover through use the Scanner object.

Code:

import java.util.Scanner;
public class SmallestElement{
  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 find smallest value
      int index = findSmallestElemIndex(numbers);  
      System.out.print("\nSmallest Element in Array: " + numbers[index]);  
  }//main
  public static int findSmallestElemIndex(int[] array){
      //Get the size of array
      int index = 0;
      //iterate over each element and print it
      for(int i = 1; i < array.length; ++i){
           if(array[index] > array[i]){
                index = i;
           } }
    return index;
  }
}//class

Output: 

Finding Largest Element in Array and Passing Array to Method In Java

In this, we complete a task on finding the largest element in Array and passing Array to Method In Java. We Find the largest element in the given Array through the Scanner.

Code: 

import java.util.Scanner;
public class LargestElement{
  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 find largest element
      int index = findLargestElemIndex(numbers);  
      System.out.print("\nLargest Element in Array: " + numbers[index]);  
  }//main
  public static int findLargestElemIndex(int[] array){
      //Get the size of array
      int index = 0;
      //iterate over each element and print it
      for(int i = 1; i < array.length; ++i){
           if(array[index] < array[i]){
                index = i;
           } }
    return index;
  }}//class

OutPut:

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

Navigation bar moving or Car Moving Through HTML and CSS Code

Navigation bar moving or Car Moving Through HTML and CSS Code

In this task, we move the navigation bar from left to right or right to left as you want. But in this Task, we perform an activity called car moving. car movies through HTML and CSS Code


Code:

<!DOCTYPE html>
<html>
<head>
<title>Car Moving</title>
</head>
<body style="background-color: yellow">
<marquee scrollamount="50" direction="left"><img src="car.png" width="500"></marquee>
<marquee scrollamount="50" direction="right"><img src="cr.png" width="500"></marquee>


</body>
</html>

How we set Background, Header and also make a table of services

 How we set Background, Header and also make a table of services

we create a Header and services file which shows different colors when the cursor moves on it and also attached a beautiful background color according to your choice through HTML and CSS code.

How we set Background, Header and also make a table of services

Code:

<!DOCTYPE html>

<html>

<head>

<title>Background</title>

</head>

<body >

<style>

body{

background-image: url(bac.jpg);

background-repeat: no-repeat;

background-size: cover;

background-attachment: fixed;


}

ul{

list-style-type: none;

background-color: black;

height: 50px;

padding-top: 15px;

}

table,tr,td,th{

border-collapse: collapse;

border:2px solid black;

font-size: 25px;

}

li{

display: inline;

padding-left: 20px;

padding-bottom: 20px;

}

a{

text-decoration: none;

color: white;

}

td:hover{

cursor: pointer;

color: red;

}

.row1:hover{

color: blue;

cursor: pointer;

background-color: pink;

}

.row2:hover{

cursor: pointer;

color: red;

background-color: yellow;

}

.row3:hover{

cursor: pointer;

color: blue;

background-color: skyblue;

}

.row4:hover{

cursor: pointer;

background-color: black;

color: white;

}

.row5:hover{

cursor: pointer;

background-color: pink;

color:green;

}

#div1{

height: 200px;

margin-top: 50px;

font-size: 150px;

text-align: center;

background-color: white;

opacity: 0.5;


}

</style>

<ul>

<li><a href="file:///C:/Users/AjWa%20Lp/Desktop/carmoving.html">Home</a></li>

<li><a href="resultcard.html">About</a></li>

<li><a href="#">services</a></li>

</ul>

<table width="100%">

<tr class="row1">

<td>1.</td>

<td>Services 1</td>

</tr>

<tr class="row2">

<td>2.</td>

<td>Services 2</td>

</tr>

<tr class="row3">

<td>3.</td>

<td>Services 3</td>

</tr>

<tr class="row4">

<td>4.</td>

<td>Services 4</td>

</tr>

<tr class="row5">

<td>5.</td>

<td>Services 5</td>

</tr>

</table>

<div id="div1">Services</div>


</body>

</html>

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