BDT Lab 1and2 22mis1067
BDT Lab 1and2 22mis1067
22MIS1067
BDT Lab 1
1. Write a Java program to check whether the given number is prime or not.
Code:
import java.util.Scanner;
Output:
2. Write a Java program to do word count program for Para.txt file.
Para.txt:
Code:
import java.io.FileReader;
import java.io.BufferedReader;
class Java22MIS1067{
public static void main(String args[]){
String l;
int count=0;
try{
BufferedReader br= new BufferedReader(new FileReader("Para.txt"));
while((l=br.readLine())!=null){
String words[]=l.split(" ");
count+=words.length;
}
System.out.println();
System.out.println("No. of words : " + count);
System.out.println();
br.close();
}catch(Exception e){System.out.println(e);}
}
}
Output:
3. Create a array list of integer type and perform add, display operations.
Code:
import java.util.ArrayList;
public class IntAL22MIS1067 {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>();
int sum=0;
for(int i=0;i<10;i++){
int x=(int) (Math.random()*100);
al.add(x);
}
System.out.println("Array list : "+al);
System.out.print("Array List : ");
for(int i=0;i<10;i++){
System.out.print(al.get(i)+" ");
sum+=al.get(i);
}
System.out.println();
System.out.println("AL sum : "+sum);
}
}
Output:
Code:
import java.util.ArrayList;
import java.util.Comparator;
public class ALmethod22MIS1067 {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<15;i++){
int val =(int) (Math.random()*100);
al.add(val);
}
System.out.println("add method : "+al);
System.out.println("size method : "+al.size());
System.out.println("remove method : "+al.remove(5));
System.out.println("isEmpty method : "+al.isEmpty());
al.set(5, 1001);
System.out.println("set method : "+al);
al.sort(Comparator.naturalOrder());
System.out.println("sort method : "+al);
System.out.println("indexOf method : "+al.indexOf(1001));
System.out.println("get method : "+al.get(4));
al.clear();
System.out.println("clear method : "+al);
}
}
Output:
5. Write a java program to store the content of Para.txt file into an appropriate
ArrayList object as words and display the same.
Code:
import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
class J22MIS1067{
public static void main(String args[]){
String l;
int count=0,pali=0;
ArrayList<String> al = new ArrayList<>();
try{
BufferedReader br= new BufferedReader(new FileReader("Para.txt"));
while((l=br.readLine())!=null){
String words[]=l.split(" ");
count+=words.length;
for(int i=0;i<words.length;i++){
al.add(words[i]);
}
}
System.out.println();
for(int i=0;i<al.size();i++) System.out.print(al.get(i)+" ");
System.out.println("\n");
br.close();
}catch(Exception e){System.out.println(e);}
}
}
Output:
BDT Lab 2
Q> Read the student.txt file and calculate the internal mark for 60 marks as per vit norms
and display the same along with student registration number and name.
Store the details of students from the student.txt file along with internal mark in arraylist
object.
Student.txt:
regno da1 da2 da3 cat1 cat2
22MIS1001 10 10 8 47 40
22MIS1002 10 10 9 42 38
22MIS1003 9 9 9 35 40
22MIS1004 9 10 9 48 33
22MIS1005 10 10 10 50 50
Code:
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
public class Marks60_22MIS1067 {
public static void main(String[] args) {
String s;
ArrayList<ArrayList<String>> al = new ArrayList<ArrayList<String>>();
try{
int i=0;
BufferedReader br = new BufferedReader(new FileReader("student.txt"));
while((s=br.readLine())!=null){
al.add(new ArrayList<String>());
String words[] = s.split(" ");
for(int j=0;j<words.length;j++){
al.get(i).add(j,words[j]);
}
i++;
}
}catch(Exception e){System.out.println(e);}
ArrayList<String> total = new ArrayList<>();
double totalMarks = 0;
for(int i=0;i<al.size();i++){
if(i==0) total.add("Total");
else{
for(int j=1;j<al.get(0).size();j++){
if(j<4) totalMarks+= Integer.parseInt(al.get(i).get(j));
else totalMarks+= (Integer.parseInt(al.get(i).get(j)))*0.3;
}
total.add(Double.toString(totalMarks));
totalMarks=0;
}
}
for(int i=0;i<total.size();i++){
System.out.format("\n%-15s",al.get(i).get(0));
System.out.format("%10s",total.get(i)+"\n");
}
System.out.println();
}
}
Output: