Lab Practical

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Assignments

Name: Imran Ali Bhambhro

Reg No: 1812113


Practical No: 1

Objective: Get familiar with Java Environment and Integrated Development Environment
(IDE).

Description: Installation of Java Development Kit (JDK) and installing Netbeans, and write
“Hello World” program.

Installation: Java and Netabeans installation.


Hello World program:

Code:

package imran.bhambhro;

public class ImranBhambhro {

public static void main(String[] args) {

System.out.println("Hello World");

}}
Practical No: 2

Objective: Getting familiar with Java environment and writing simple programs.

Description: Writing simple programs of Binary and Ternary operators.

Code: Netbeans code

package imran.bhambhro;

import java.util.Scanner;

public class ImranBhambhro {

public static void main(String[] args) {

int a, b;

Scanner i;

i = new Scanner(System.in);

System.out.print("Enter value of A: ");

a = i.nextInt();

System.out.print("Enter value of B: ");

b = i.nextInt();

if( a==b ){System.out.println("A & B are Equal");}

else if( a!=b ){System.out.println("A & B are not Equal");}

if( a>b){System.out.println("A is greater than B");}

else if(a<b){System.out.println("A is Less than B");}

String s=(b==a)?"A and B are equal (by Ternary Op:)":"A & B are not equal (by Ternary Op:)";

System.out.println(s);

a++;

b++;

b--;

a--;}}
Screenshot
Practical No: 3

Objective: Getting familiar with conditional structures and how we use conditional structure in

different scenario.

Description: Task 1: Take input from user, and generate Electricity Bill.

Condition of Electricity bill.

100 units X 5, 200 units X 6, 300 units X 7, 400 units X 8, 500+ units X 10.

Taks 2: Take input from user, and generate student Marksheet

Task 3: Getting familiar with the control structure and write a program, take input

from user and generate factorial number.

Task 4: Getting familiar with nested loops, and conditional structures working

together and control different conditional structures, and generate

palindrome triangle.

Task 5: Write a program that can take input from user and generate stars as

square.

Task 6: Write a program that can take input from user and generate Fibonacci

series.

Task 1: Electricity Bill

Code: Netbeans code

package imran.bhambhro.pkg3;

import java.util.Scanner;

public class ImranBhambhro3 {

public static void main(String[] args) {

int unit, rms=100, total=0;

Scanner s;

System.out.print("Enter consumed units of E. Bill: ");


s = new Scanner(System.in);

unit = s.nextInt();

if(unit <= 100){total = unit*5;}

else if(unit <= 200){total = rms*5 + (unit-100)*10;}

else if(unit <= 300){total = rms*5 + 100*6 + (unit-200)*10;}

else if(unit <= 400){total = rms*5 + 100*6 + 100*7 + (unit-300)*10;}

else if(unit <= 500){total = rms*5 + 100*6 + 100*7 + 100*8 + (unit-400)*10;}

else if(unit > 500){total = rms*5 + 100*6 + 100*7 + 100*8 + (unit-400)*10;}

System.out.print("Total Bill: ");

System.out.println(total);}}

Screenshot
Task 2: Marksheet

Code: Netbeans code

package imran.bhambhro.pkg2;

import java.util.Scanner;

public class ImranBhambhro2 {

public static void main(String[] args) {

int eng, bio, math, chem, phy;

float perc,sum,total=500;

Scanner s;

Scanner t;

System.out.print("Enter student Name : ");

t = new Scanner(System.in);

String name = t.nextLine();

System.out.print("Enter student Roll N. / ID: ");

s = new Scanner(System.in);

int roll = s.nextInt();

System.out.println();

System.out.print("Enter English obtained marks: ");

s = new Scanner(System.in);

eng = s.nextInt();

System.out.print("Enter Biology obtained marks: ");

s = new Scanner(System.in);

bio = s.nextInt();

System.out.print("Enter Mathematics obtained marks: ");


s = new Scanner(System.in);

math = s.nextInt();

System.out.print("Enter Chemistry obtained marks: ");

s = new Scanner(System.in);

chem = s.nextInt();

System.out.print("Enter Physics obtained marks: ");

s = new Scanner(System.in);

phy = s.nextInt();

System.out.println();

System.out.println("Name: "+name+" ("+roll+")");

System.out.println();

System.out.println("Sub Total Obt Grade");

if(eng < 10){System.out.println("English 100 " + eng + " Fail");}

else if(eng < 40){System.out.println("English 100 " + eng + " Fail");}

else if(eng < 50){System.out.println("English 100 " + eng + " D");}

else if(eng < 60){System.out.println("English 100 " + eng + " C");}

else if(eng < 70){System.out.println("English 100 " + eng + " B");}

else if(eng < 80){System.out.println("English 100 " + eng + " A");}

else if(eng <= 100){System.out.println("English 100 " + eng + " A+");}

if(bio < 10){System.out.println("Biology 100 " + bio + " Fail");}

else if(bio < 40){System.out.println("Biology 100 " + bio + " Fail");}

else if(bio < 50){System.out.println("Biology 100 " + bio + " D");}

else if(bio < 60){System.out.println("Biology 100 " + bio + " C");}

else if(bio < 70){System.out.println("Biology 100 " + bio + " B");}

else if(bio < 80){System.out.println("Biology 100 " + bio + " A");}


else if(bio <= 100){System.out.println("Biology 100 " + bio + " A+");}

if(math < 10){System.out.println("Mathematics 100 " + math + " Fail");}

else if(math < 40){System.out.println("Mathematics 100 " + math + " Fail");}

else if(math < 50){System.out.println("Mathematics 100 " + math + " D");}

else if(math < 60){System.out.println("Mathematics 100 " + math + " C");}

else if(math < 70){System.out.println("Mathematics 100 " + math + " B");}

else if(math < 80){System.out.println("Mathematics 100 " + math + " A");}

else if(math <= 100){System.out.println("Mathematics 100 " + math + " A+");}

if(chem < 10){System.out.println("Chemistry 100 " + chem + " Fail");}

else if(chem < 40){System.out.println("Chemistry 100 " + chem + " Fail");}

else if(chem < 50){System.out.println("Chemistry 100 " + chem + " D");}

else if(chem < 60){System.out.println("Chemistry 100 " + chem + " C");}

else if(chem < 70){System.out.println("Chemistry 100 " + chem + " B");}

else if(chem < 80){System.out.println("Chemistry 100 " + chem + " A");}

else if(chem <= 100){System.out.println("Chemistry 100 " + chem + " A+");}

if(phy < 10){System.out.println("Physics 100 " + phy + " Fail");}

else if(phy < 40){System.out.println("Physics 100 " + phy + " Fail");}

else if(phy < 50){System.out.println("Physics 100 " + phy + " D");}

else if(phy < 60){System.out.println("Physics 100 " + phy + " C");}

else if(phy < 70){System.out.println("Physics 100 " + phy + " B");}

else if(phy < 80){System.out.println("Physics 100 " + phy + " A");}

else if(phy <= 100){System.out.println("Physics 100 " + phy + " A+");}

else{System.out.println("Error in entered marks.");}


System.out.println();

sum = eng+math+bio+phy+chem;

perc=sum*100/total;

System.out.println("Percentage: "+perc);}}

Screenshot
Task 3: Factorial Number

Code:

package factorial;

import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {

System.out.print("Enter any Number: ");

System.out.println(fac());

System.out.println("Imran Ali Bhambhro (1812113)");}

private static long fac(){

long b=1;

Scanner sc = new Scanner(System.in);

long a = sc.nextInt();

for(int i=1; i<=a; i++){

b*=i;

return b;

}}
Screenshot
Task 4: Palindrome Triangle

Netbeans code

package imran.bhambhro.pkg4;

import java.util.Scanner;

public class ImranBhambhro4 {

public static void main(String[] args) {

System.out.print("Enter Height of the Pyramid : ");

Scanner s = new Scanner(System.in);

int n = s.nextInt();

for (int i=1; i<=n;i++) {

for (int j=i; j<n; j++)

System.out.print(" ");

for (int j=1; j<=i; j++)

System.out.print(j);

for (int j= i-1; j>=1; j--)

System.out.print(j);

System.out.println();}}}
Screenshot

Task 6: Fabonacci Series

Code:

package imranbhambhro5;

import java.util.Scanner;

public class Imranbhambhro5 {

public static void main(String[] args) {

System.out.print("How many Fibonacci numbers you want? : ");

Scanner sc = new Scanner(System.in);

long n = sc.nextInt();

long a=0,b=1,total;

for(int i=1; i<=n; i++)

{total = a+b;
System.out.print(total + " ");

a=b;

b=total;}

System.out.println();}}

Screenshot

You might also like