Wednesday, 26 December 2018

CSE1007 - Wintersem 2018-19

Question- Slow learner problem
Write a program to demonstrate the knowledge of students in multidimensional arrays and looping
constructs.
Eg., If there are 4 batches in BTech - “CSE1007” 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.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package practice.apps;

/**
 *
 * @author Naman Khurpia
 */
import java.util.Scanner;
public class faltu
{
    public static void main(String arr[])
    {
        Scanner b= new Scanner(System.in);
        int content,z;
        int ans=0;

        int a[][]=new int[4][];
        for(int i=0;i<4;i++)
        {
            System.out.println("Enter the number of slow learners in the batch "+i);
            content=b.nextInt();
            if(content%4==0)
                z=content/4;
            else
                z=content/4+1;
         
            a[i]=new int[z];

            for(int j=0;j<z;j++)
            {
                if(content>4)
                {
                    a[i][j]=4;
                    content=content-4;
                }
                else
                {
                    a[i][j]=content;
                    content=0;
                }
            }
        }
        System.out.println("\n\nThe Contents of Jagged array are");
        for(int i=0;i<4;i++)
        {
            for(int n:a[i])
            {
                System.out.print(n);
                if(n==4)
                    ans++;
            }
            System.out.println();
        }
        System.out.println("\n\nThe number of Tutors with 4 students are (ans) = "+ans);

    }

}




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


Solution-
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package practice.apps;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;
/**
 *
 * @author Naman Khurpia
 */
class chemicals{
ArrayList<String> arrl1 = new ArrayList<String>(50); ArrayList<String> arrl2 = new
ArrayList<String>(50);
void add(String substring) {
throw new UnsupportedOperationException("Not supported yet."); //To change body ofgenerated methods, choose Tools | Templates.
}
}
public class moles {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String equation = scan.nextLine();
String reactants = " ";
String prod = " ";
for(int i=0;i<equation.length();i++){
if((equation.charAt(i)=='-') && (equation.charAt(i+1)=='>')){
reactants = equation.substring(0, i);
reactants = reactants.replaceAll("\\s","");
enter(reactants,"Reactants");
prod = equation.substring(i+2,equation.length());
prod = prod.replaceAll("\\s","");
enter(prod,"Product");
}
}
}
public static void enter(String s,String tpe){
chemicals total = new chemicals();
for(int i=0;i<s.length()-1;i++){
for(int j=i+1;j<s.length();j++){
if((s.charAt(j)=='+')){
String check=s.substring(i,j);
if(check.charAt(check.length()-1)=='+')
total.arrl1.add(check.substring(0,check.length()));
else
total.arrl1.add(check.substring(0,check.length()));
i=j+1;
}
else if((s.length()-1==j)){
String check2=s.substring(i,j+1);
if(check2.charAt(check2.length()-1)=='+')
total.arrl1.add(check2.substring(0,check2.length()));
else
total.arrl1.add(check2.substring(0,check2.length()));
i=j+1;
}
}
}
System.out.print( tpe+" are ");
for(int i=0;i<total.arrl1.size();i++){
Pattern p = Pattern.compile("^\\d*");
Matcher m = p.matcher(total.arrl1.get(i));
if (m.find()) {
if(!m.group().equals(""))
System.out.print(m.group(0)+" moles of"+total.arrl1.get(i).substring(m.group(0).length(),total.arrl1.get(i).length())+" ");
else
System.out.print("1 moles of "+total.arrl1.get(i)+" ");
}
}
System.out.println();
}
}



Screenshot-

No comments:

Post a Comment

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...