Vous êtes sur la page 1sur 14

JAVA PROGRAMMING LAB

CSE1007
NAME – MOHIT AJMERA
REG. NO. – 16BIT0156
SLOT – L41+L42
FACULTY – Prof. GUNDALA SWATHI

Assessment – 1

1. Write a program to demonstrate the knowledge of students in basic Java concepts.


Eg., Write a program to read the First name and Last name of a person, his weight
and height using command line arguments. Calculate the BMI Index which is defined
as the individual's body mass divided by the square of their height.
Category BMI Range-Kg/m2
Underweight <18.5
Normal (healthy weight) 18.5 to 25
Overweight 25 to 30
Obese Class Over 30

Display the name and display his category based on the BMI value thus calculated.

CODE –

import java.util.*;

import java.lang.*;

import java.io.*;

public class one

public static void main(String args[])

System.out.println("\n\t\tWelcome MOHIT AJMERA - 16BIT0156\n");

Scanner sc = new Scanner(System.in);

System.out.println("Enter the First and Last name of person (Sepreated by space) :");
String x = sc.next();

String p = sc.next();

System.out.println("Enter the Height of "+x+" "+p+" Person (in cm): ");

double y = sc.nextDouble();

System.out.println("Enter the Weight of "+x+" "+p+" Person (in kg): ");

double z = sc.nextDouble();

double b = z/((y/100)*(y/100));

String bmi = Double.toString(b)+ " Kg/m2";

String a;

if(b < 18.5)

a = "Underweight";

else if(b>=18.5 && b<=25)

a = "Normal (healthy weight)";

else if(b>25 && b<=30)

a = "Overweight";

else

a = "Obese Class";

System.out.println("\n"+x+" "+p+" :"+" "+a+" "+"BMI - "+bmi);

OUTPUT –
2. Write a program to demonstrate the knowledge of students in multidimensional
arrays and looping constructs.
Eg., If there are 4 batches in BTech(IT) learning ‘ITE2005’ course, read the count
of the slow learners (who have scored <25) in each batch. Tutors should be assigned
in the ratio of 1:4 (For every 4 slow learners, there should be one tutor). Determine
the number of tutors for each batch. Create a 2-D jagged array with 4 rows to store
the count of slow learners in the 4 batches. The number of columns in each row should
be equal to the number of groups formed for that particular batch ( Eg., If there are
23 slow learners in a batch, then there should be 6 tutors and in the jagged array, the
corresponding row should store 4, 4, 4, 4, 4,3). Use for-each loop to traverse the array
and print the details. Also print the number of batches in which all tutors have exactly
4 students.
CODE –

import java.util.*;

import java.lang.*;

public class two

public static void main(String[] args) {

int i, j;

double t;

System.out.println("\n\t\tWelcome MOHIT AJMERA - 16BIT0156\n");

int arr[][] = new int[4][]; // Declaring 2-D array with 4 rows

Scanner sc = new Scanner(System.in); // input for each batch

for(i = 0; i < arr.length; i++)

System.out.print("Enter number of students for batch " + (i+1) + ": ");

t = sc.nextDouble();

arr[i] = new int[(int)Math.ceil(t/4)];

for(j = 0; j < arr[i].length; j++)

if(t >= 4)

arr[i][j] = 4;

else

arr[i][j] = (int)t;

t = t - 4;

// Displaying the values of 2D Jagged array

int cf = 0;

System.out.println("\n2D Jagged Array Contents are -\n");


for(int[] m:arr)

for(int k:m)

System.out.print(k+ " ");

if(k==4)

cf++;

System.out.println();

System.out.println("\nNumber of tutors with 4 students are: " + cf );

OUTPUT –
3. Write a program to demonstrate the knowledge of students in String handling.
Eg., Write a program to read a chemical equation and find out the count of the
reactants and the products. Also display the count of the number of molecules of
each reactant and product.
Eg., For the equation, 2NaOH + H2SO4 -> Na2SO4+ 2H2O, the O/P should be as
follows.

Reactants are 2 moles of NaOH, 1 mole of H2SO4.

Products are 1 mole of Na2SO4 and 2 moles of H2O.

CODE –

import java.util.*;

import java.lang.*;

import java.io.*;

public class three

public static void main(String args[])

{
System.out.println("\n\t\tWelcome MOHIT AJMERA - 16BIT0156\n");

Scanner sc = new Scanner(System.in);

System.out.println("Enter the Equation");

String a = sc.nextLine();

System.out.println("\r\n");

String[] b = a.split(" -> ",2);

String y = new String("Reactants are ");

for(String i:b)

String[] c = i.split(" [+] ",5);

for(String j:c)

char x = j.charAt(0);

int m =0;

if(Character.isDigit(x)==true)

m = Character.getNumericValue(x);

y=y+m+" mole of "+j.substring(1)+", ";

else

m=1;

y=y+m+" mole of "+j+", ";

String z = y.substring(0,y.length()-2);

System.out.println(z+".");

y="";

y=y+"Products are ";

OUTPUT –
4. Write a program to demonstrate the knowledge of students in advanced concepts of
Java string handling.
Eg., (Bioinformatics: finding genes) Biologists use a sequence of letters A, C, T, and
G to model a genome. A gene is a substring of a genome that starts after a triplet
ATG and ends before a triplet TAG, TAA, or TGA. Furthermore, the length of a
gene string is a multiple of 3 and the gene does not contain any of the triplets ATG,
TAG, TAA, and TGA. Write a program that prompts the user to enter a genome
and displays all genes in the genome. If no gene is found in the input sequence,
displays no gene. Here are the sample runs:

Enter a genome string: TTATGTTTTAAGGATGGGGCGTTAGTT

O/P: TTT

GGGCGT

CODE –

import java.util.*;
import java.lang.*;

import java.io.*;

public class four

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the String");

String a = sc.nextLine();

for(int i=0;i<a.length()-2;i++)

String b = a.substring(i,i+3);

if(b.equals("ATG"))

int x;

x = a.indexOf(b,i);

System.out.println(x);

aaa:

for(int j=i+3;j<a.length()-2;j++)

String c = a.substring(j,j+3);

if(c.equals("TAG") || c.equals("TAA") || c.equals("TGA"))

int y;

y = a.indexOf(c,j);

System.out.println(y);

String z = a.substring(x+3,y);

if(y-x-3!=0)

for(int k=0;k<z.length()-2;k++)

String e = z.substring(k,k+3);
if(e.equals("ATG") || e.equals("TAG") || e.equals("TAA") ||
e.equals("TGA"))

break aaa;

System.out.println(z);

break aaa;

else

System.out.println("No Gene");

OUTPUT –
5. Write a program to demonstrate the knowledge of students in working with classes
and objects.
Eg.,Create a class Film with string objects which stores name, language and
lead_actor and category (action/drama/fiction/comedy). Also include an integer data
member that stores the duration of the film. Include parameterized constructor,
default constructor and accessory functions to film class. Flim objects can be
initialized either using a constructor or accessor functions. Create a class FilmMain
that includes a main function. In the main function create a vector object that stores
the information about the film as objects. Use the suitable methods of vector class to
iterate the vector object to display the following

a. The English film(s) that has Arnold as its lead actor and that runs for
shortest duration.

b. The Tamil film(s) with Rajini as lead actor.

c. All the comedy movies.

CODE –

class Film{

String name;

String language;

String lead_actor;

String category;

int duration;

Film(){

name = null;

language = null;

lead_actor = null;

category = null;

duration = 0;

Film(String n, String l, String a, String c, int d){


name = n;

language = l;

lead_actor = a;

category = c;

duration = d;

public class FlimMain {

public static void main(String[] args) {

System.out.println("\n\t\tWelcome MOHIT AJMERA - 16BIT0156\n");

Film[] films = new Film[5];

films[0] = new Film();

films[0].name = "Golmal Again";

films[0].language = "Hindi";

films[0].lead_actor = "Ajay";

films[0].duration = 8000;

films[0].category = "Comedy";

films[1] = new Film();

films[1].name = "Terminator";

films[1].language = "English";

films[1].lead_actor = "Arnold";

films[1].duration = 6000;

films[1].category = "Action";

films[2] = new Film();

films[2].name = "Robot 1";

films[2].language = "Tamil";

films[2].lead_actor = "Rajini";

films[2].duration = 7000;

films[2].category = "Comedy";

films[3] = new Film();


films[3].name = "Terminator 2";

films[3].language = "English";

films[3].lead_actor = "Arnold";

films[3].duration = 5000;

films[3].category = "Drama";

films[4] = new Film();

films[4].name = "Robot 2";

films[4].language = "Tamil";

films[4].lead_actor = "Rajini";

films[4].duration = 8888;

films[4].category = "fiction";

int min = 9999;

int jmin = 0;

System.out.println("\n");

System.out.println("(a):");

for(int j = 0; j < 5; j++)

if(films[j].lead_actor.equals("Arnold"))

if(films[j].duration < min){

min = films[j].duration;

jmin = j;

System.out.println("Shortest Arnold film is " + films[jmin].name + " and duration is " + min + "
Seconds.");

System.out.println("\n");

System.out.println("(b):");

for(int j = 0; j < 5; j++)

if(films[j].lead_actor.equals("Rajini") && films[j].language.equals("Tamil"))

System.out.println("Tamil movie is " + films[j].name + " with Rajini as lead Actor.");

}
}

System.out.println("\n");

System.out.println("(c):");

for(int j = 0; j < 5; j++)

if(films[j].category.equals("Comedy"))

System.out.println("Comedy movie: " + films[j].name);

OUTPUT –

Vous aimerez peut-être aussi