11 Jan 2019

CSE1007: Java. Winter Semester 2018-19 lab problems.


-------------------------------------------
So, i used to run this blog back in 2nd semester, for skillrack problems for the OOPS with C++ course, now in the 6th semester i find that a lot of us have been coerced into taking the java course and the lab problems being given by professors are to a significant degree the same for all classes...
   ...So instead of sending my files to friends individually i've decided to dig up this forgotten grave of a blog and make one last post that i will keep updating over this semester as i do my lab tasks. 
    I will be following the cycle sheet that is being followed in my class and i'm told in some other classes too, and some professors that are not following it also happen to have coinciding questions so you will probably find some of your assignments here if not all. if however there is a popular question in other classes not being given in my class, DM me or drop in in the comments and i'll try to do that as well. 
Tl;dr = will post my java codes here; you may find most or some of what you need; dm/comment for requests; jet fuel can't melt steel beams; i used lower case i for the pronoun and this font which makes me artsy af (thats how it works just ask rupi kaur). 
-----------------------------------------

Nuff said..

 

----Question 1----

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.io.*;
import java.util.Scanner;
class ct0{
    public static void main(String[] args){
        float bmi=Float.parseFloat(args[1])/Float.parseFloat(args[2])/Float.parseFloat(args[2]);
         String c;
        if (bmi<18.5
            c="Underweight";
        else if (bmi<25)
            c="Normal";
        else if (bmi<30)
            c="overweight";
        else
            c="obese";
        System.out.println(args[0]+" "+c);
    }
}

Output:


 

----Question 2----

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.io.*;
import java.util.Scanner;
class ct1{
    public static void main(String[] args){
        System.out.println(Integer.parseInt("10")+Integer.valueOf("10a"));
        Scanner in=new Scanner(System.in);
        int i,n,x,j;
        int[][] ja=new int[4][];
         for( i=0;i<4; i++){
            System.out.println("input number of students in batch"+(i+1));
            x=in.nextInt();
            n=(x/4)+((x/4)*4==x?0:1);
            ja[i]=new int[n];
            for(j=0;j<n;j++,x-=4) ja[i][j]=x>4?4:x;
        }
        System.out.println("array made, now we parse:");
        int tot=0;
        for( i=0;i<4; i++){
            System.out.println("");
            System.out.println("batch "+(i+1));
            for(j=0;j<ja[i].length;j++){
                System.out.print(ja[i][j]+", ");
                if(ja[i][j]==4) tot++;
            }
        }
        System.out.println("total number of full groups is:"+tot);
    }
}

Output:

----Question 3----

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.io.*;
import java.util.Scanner;


class ct2{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        String e=in.nextLine();
        int i;
        System.out.print("Reactants are:");
        for(String p : e.split("-")[0].split(" ")){
            if(p.charAt(0)>='A' && p.charAt(0)<='Z') System.out.print("1 mole of "+p);
            else if(p.charAt(0)>='0' && p.charAt(0)<='9'){
                for(i=0;i<p.length() && p.charAt(i)>='0' && p.charAt(i)<='9';i++);
                System.out.print(p.substring(0,i));
                System.out.print(" mole of "+p.substring(i)+", ");
            }
        }
        System.out.print("\n Products are:");
        for(String p : e.split("-")[1].split(" ")){
            if(p.charAt(0)>='A' && p.charAt(0)<='Z') System.out.print("1 mole of "+p);
            else if(p.charAt(0)>='0' && p.charAt(0)<='9'){
                for(i=0;i<p.length() && p.charAt(i)>='0' && p.charAt(i)<='9';i++);
                System.out.print(p.substring(0,i));
                System.out.print(" mole of "+p.substring(i)+", ");
            }
        }
        System.out.print("\n");
    }
}

Output: