0% found this document useful (0 votes)
13 views8 pages

BDT Lab 1and2 22mis1067

The document contains Java programs for various tasks including checking if a number is prime, counting words in a text file, manipulating an ArrayList, and calculating internal marks from student data. Each task is accompanied by code snippets and sample outputs. The document serves as a practical guide for implementing basic Java programming concepts.

Uploaded by

kaustubhcool127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

BDT Lab 1and2 22mis1067

The document contains Java programs for various tasks including checking if a number is prime, counting words in a text file, manipulating an ArrayList, and calculating internal marks from student data. Each task is accompanied by code snippets and sample outputs. The document serves as a practical guide for implementing basic Java programming concepts.

Uploaded by

kaustubhcool127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Kaustubh Bharti

22MIS1067
BDT Lab 1
1. Write a Java program to check whether the given number is prime or not.

Code:

import java.util.Scanner;

public class Prime22MIS1067 {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number : ");
int num = scan.nextInt();
int i=2;
while(i<num){
if(num%i==0) break;
i++;
}
if(i==num) System.out.println(num+" is a prime number.");
else System.out.println(num+" is not a prime number.");
}
}

Output:
2. Write a Java program to do word count program for Para.txt file.

Para.txt:

Sore was I ere I saw Eros


A man a plan a canal Panama
Never a foot too far even
Euston saw I was not Sue
Live on evasions No I save no evil
Red Roses run no risk sir on nurses order
Salisbury moor sir is roomy Rub Silas
Marge lets went I await news telegram
A new order began a more Roman age bred Rowena
I man am regal a German am I
Tracy no panic in a ponycart
Egad Loretta has Adams as mad as a hatter Old age
Eve mad Adam Eve
Resume so pacific a pose muser
Marge let a moody baby doom a telegram
Tenet C is a basis a basic tenet
Nellas simple hymn I attain my help Miss Allen
Straw No too stupid a fad I put soot on warts
Sir I demand I am a maid named Iris
Lay a wallaby baby ball away Al
Tessas in Italy Latin is asset
Noel sees Leon
No it can assess an action

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:

4. Explore ArrayList methods using suitable java program

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:

You might also like