SCHOOL OF ADVANCED SCIENCES Winter Semester 2018-19 Lab Assessment –I Name: GOPAL GUPTA Faculty: Prof. Thippa Reddy Course: Java Programming
Registration No: 16BIT0276 Slot: L41 + L42 Code: CSE1007
Code: import java.util.*; class A { public static void main(String arr[]) { Scanner b= new Scanner(System.in); int x,z; int ans=0; //Jagged array initialized
1
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); x=b.nextInt(); if(x%4==0) z=x/4; else z=x/4+1; //Individual Row of jagged array initialized a[i]=new int[z];
for(int j=0;j
4) {a[i][j]=4;x=x-4;} else {a[i][j]=x;x=0;} } } System.out.println("\n\nThe Contents of Jagged array are");
for(int i=0;i<4;i++) {
2
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);
}
}
OUTPUT:
3
Code: import java.util.*; import java.io.*; public class Chemeqn { public static void main(String[]args) { System.out.println("----------Give The Chemical Equation---------------"); Scanner scan = new Scanner(System.in); String s=new String(); s=scan.nextLine(); s=" "+s; String s1=new String(); String s2="QWERTYUIOPASDFGHJKLZXCVBNM"; for(int i=0;i<=s.length()-2;i++) { char ch=s.charAt(i); char ch1=s.charAt(i+1); 4
if(ch == ' ' && ch1 != '+' && ch1 != '-') { for(int j=i+1;j<=s.length()-1;j++) { char ch2=s.charAt(j); if(s2.indexOf(ch2)==-1) { System.out.print(ch2); } else { i=j; break; } } } } }
}
5
SCHOOL OF ADVANCED SCIENCES Winter Semester 2018-19 Lab Assessment – II Name: GOPAL GUPTA Faculty: Prof. Thippa Reddy Course: Java Programming
Registration No: 16BIT0276 Slot: L41 + L42 Code: CSE1007
Code: import java.util.*; import java.io.*;
class BMI { public static void main(String args[]) { String fname = args[0]; 1
String lname = args[1]; float ht = Float.parseFloat(args[2]); int wt = Integer.parseInt(args[3]); float bmi = (wt)/(ht*ht); System.out.println("Name : "+fname+" "+lname); System.out.println("BMI : "+bmi); if(bmi<18.5) { System.out.println("BMI category : Underweight"); } else if(bmi>=18.5 && bmi<25) { System.out.println("BMI category : Normal (healthy weight)"); } else if(bmi>=25 && bmi<30) { System.out.println("BMI category : Overweight"); } else { System.out.println("BMI category : Obese"); } } } 2
INPUT:
OUTPUT:
3
Code: import java.util.*; public class gene { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a genome string: "); String genome = sc.nextLine(); boolean found = false; int start = -1; for (int i = 0; i < genome.length() - 2; i++) { String triplet = genome.substring(i, i + 3); 4
if (triplet.equals("ATG")) { start = i + 3; } else if (((triplet.equals("TAG")) || (triplet.equals("TAA")) || (triplet.equals("TGA")))&& (start != -1)) { String gene = genome.substring(start, i); if (gene.length() % 3 == 0) { found = true; System.out.println(gene); start = -1; } } } if (!found) System.out.println("no gene is found"); } }
5
OUTPUT:
6
SCHOOL OF ADVANCED SCIENCES Winter Semester 2018-19 Lab Assessment – III Name: Gopal Gupta Faculty: Prof. Thippa Reddy Course: Java Programming
Registration No: 16BIT0276 Slot: L41 + L42 Code: CSE1007
1
CODE: import java.util.*; import java.io.*;
class Film { String name; String lang; String lead_actor; String category; int duration; Film(String a,String b,String c,String d,int e) { name = a; lang = b; lead_actor = c; category = d; duration = e; } Film() { name = lang = lead_actor = category = null; duration = 0; }
2
void findarnold() { if(this.lead_actor=="Arnold") { System.out.println("Name : "+this.name); System.out.println("Lead Actor : "+this.lead_actor); System.out.println("Language : "+this.lang); System.out.println("Category : "+this.category); System.out.println("Duration : "+this.duration); System.out.println(); } } void findtamil() { if(this.lang=="Tamil" && this.lead_actor=="Rajni") { System.out.println("Name : "+this.name); System.out.println("Lead Actor : "+this.lead_actor); System.out.println("Language : "+this.lang); System.out.println("Category : "+this.category); System.out.println("Duration : "+this.duration); System.out.println(); } }
3
void findcomedy() { if(this.category=="Comedy") { System.out.println("Name : "+this.name); System.out.println("Lead Actor : "+this.lead_actor); System.out.println("Language : "+this.lang); System.out.println("Category : "+this.category); System.out.println("Duration : "+this.duration); System.out.println(); } } }
class FilmMain { public static void main(String args[]) { int i; Film[] f = new Film[5]; f[0] = new Film("Robot 2.0","Tamil","Rajni","Action",3); f[1] = new Film("Dhamaal","Hindi","Circuit","Comedy",2); f[2] = new Film("Terminator","English","Arnold","Action",2); f[3] = new Film("Padmaavat","Hindi","Ranveer Singh","Drama",3); 4
f[4] = new Film("Robot 1.0","Tamil","Rajni","Action",3);
for(i=0;i<5;i++) f[i].findarnold(); for(i=0;i<5;i++) f[i].findtamil(); for(i=0;i<5;i++) f[i].findcomedy(); } }
OUTPUT:
5
CODE: import java.util.Scanner; public interface themepark { int adultfee=500; int childfee=300; void buyticket(int n,int m); void playGame(int a); void count(); public static void main(String args[]) { System.out.println("Which theme park? Queensland or Veegeland?"); Scanner scr=new Scanner(System.in); String theme=scr.nextLine(); System.out.println("Enter no. of adults"); int n=scr.nextInt(); System.out.println("Enter no. of children"); int m=scr.nextInt(); String choice; int game; if("Queensland".equalsIgnoreCase(theme)) { Queensland t2; t2=new Queensland(); t2.buyticket(n,m); 6
System.out.println("Which game do you want to play?"); game=scr.nextInt(); t2.playGame(game); do { System.out.println("Do you want to play another game?"); choice=scr.next(); if(choice.equalsIgnoreCase("yes")) { System.out.println("Which game do you want to play?"); game=scr.nextInt(); t2.playGame(game); } }while(!"no".equalsIgnoreCase(choice)); t2.count(); } else if("Veegeland".equalsIgnoreCase(theme)) { Veegeland t3; t3 = new Veegeland(); t3.buyticket(n,m); System.out.println("Which game do you want to play?"); game=scr.nextInt(); t3.playGame(game); do { System.out.println("Do you want to play another game?"); choice=scr.next(); if(choice.equalsIgnoreCase("yes")) { System.out.println("Which game do you want to play?"); game=scr.nextInt(); t3.playGame(game); } }while(!"no".equalsIgnoreCase(choice)); t3.count(); } } } class Queensland implements themepark { Queensland() { 7
this.Games = new boolean[30]; for(int i=0;i<30;i++) { Games[i]=false; } } @Override public void buyticket(int n,int m) { int totalcost=n*adultfee+childfee*m; System.out.println("The ticket cost is "+totalcost); } boolean Games[]; @Override public void playGame(int a) { if(a>30) { System.out.println("Games till 30 are only available"); } else if(Games[a-1]==false) { Games[a-1]=true; } else if(Games[a-1]==true) { System.out.println("You have already played this game."); } } @Override public void count() { int count=0; for(int j=0;j<30;j++) { if(Games[j]==true) { count++; } } System.out.println("No. of games played is "+count); System.out.println("No. of games left is "+(30-count)); } } 8
class Veegeland implements themepark{ int Games[]; Veegeland() { Games=new int[40]; int j; for(j=0;j<40;j++) { Games[j]=0; } } @Override public void buyticket(int n,int m) { int totalcost=n*adultfee+childfee*m; System.out.println("The ticket cost is "+totalcost); } @Override public void playGame(int a) { if(a<=40) { Games[a-1]=Games[a-1]+1; } else { System.out.println("Only games till 40 are available"); } } @Override public void count() { int count=0; int countmorethanone=0; for(int i=0;i<40;i++) { if(Games[i]>0) { count++; } if(Games[i]>1) { countmorethanone++; 9
} } System.out.println("No. of games played is "+count); System.out.println("No. of games played for more than one time are "+countmorethanone); System.out.println("No. of games not played is "+(40-count)); } }
OUTPUT:
10
SCHOOL OF ADVANCED SCIENCES Winter Semester 2018-19 Lab Assessment – IV Name: Gopal Gupta Faculty: Prof. Thippa Reddy Course: Java Programming
Registration No: 16BIT0276 Slot: L41 + L42 Code: CSE1007
CODE: Prime.java Package primespackage; public class Prime { public static boolean checkForPrime(int n) { boolean flag=true; for(int i=2;i
if(n%i==0) { flag=false; break; } } If(n==1) return false; return flag; } } TwinPrimes.java Import primes.package.*; import java.util.*; class TwinPrimes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the starting number"); int x = sc.nextInt(); System.out.println("Enter the ending number"); int y = sc.nextInt(); for(int i=x;i<=y;i++)
2
{ if(Prime.checkForPrime(i)) { if(Prime.checkForPrime(i+2)) System.out.println("("+i+","+(i+2)+")"); } } } }
OUTPUT:
3
SCHOOL OF ADVANCED SCIENCES Winter Semester 2018-19 Lab Assessment – V Name: Gopal Gupta Faculty: Prof. Thippa Reddy Course: Java Programming
Registration No: 16BIT0276 Slot: L41 + L42 Code: CSE1007
CODE: import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher;
public class exception{
public static void main(String args[]){ Scanner s = new Scanner(System.in); System.out.println("Enter your registration number: "); String r = s.nextLine(); System.out.println("Enter your mobile number: "); 1
String m = s.nextLine(); try{ if(r.length()!=9) throw new IllegalArgumentException("Not Valid Registration Number"); else System.out.println("Valid Registration Number"); if(m.length()!=10) throw new IllegalArgumentException("Not Valid Mobile Number"); else System.out.println("Valid Mobile Number"); } catch(IllegalArgumentException e) { System.out.println(e); } try{ if(Pattern.matches("[^0-9]", m)) throw new NumberFormatException("Not Valid"); else System.out.println("Number conatins all digits"); } catch(NumberFormatException n) { 2
System.out.println(n); } try{ if(Pattern.matches("[^A-Z][^a-z][^0-9]", r)) throw new NoSuchElementException ("Not Valid"); else System.out.println("Registration number contains no character"); } catch(NoSuchElementException w) { System.out.println(w); }}}
OUTPUT:
3
CODE: import java.io.*; import java.util.*; class Donor implements Serializable{ String name; int age; String address; String contact; String group; int dold; Donor(String name,int age,String address,String contact,String group,int dold){ this.name=name; this.age=age; this.address=address; this.contact=contact; this.group=group; this.dold=dold; } public String toString(){ 4
return "name is "+name+" age is "+age+" address is "+address; } }
public class DonorMain{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the number of donors"); int n = sc.nextInt(); sc.nextLine(); try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("1.txt")); Donor[] d=new Donor[n]; for(int i=0;i
System.out.println("Enter group"); String group=sc.nextLine(); System.out.println("Enter date of last donation"); int dold= sc.nextInt(); sc.nextLine(); d[i] = new Donor(name,age,address,contact,group,dold); oos.writeObject(d[i]); } } catch(IOException e){ System.out.println(e); } try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("1.txt")); for(int i=0;i=6) System.out.println(d); } }catch(Exception e){ System.out.println(e); } } 6
}
OUTPUT:
7
LAB ASSESSMENT Gopal Gupta 16BIT0276 /* * 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 javaapplication1; import java.lang.*; /** * * @author guest-1i7MWe */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { int i; String S1= "hai hai hello how are you"; String S2= "hai hello I am fine"; String[] word = S2.split(" "); String[] words = S1.split(" "); int wrc=1; int urc=0; double[] freq= new double[100]; double[] freqc= new double[100]; for (i=0;i<words.length;i++) { for(int j=i+1;j<words.length;j++) { if(words[i].equals(words[j])) { wrc=wrc+1; words[j]="0"; } } for(int k=0;k<word.length;k++) { if(words[i].equals(word[k])) { urc = urc+1; word[k]="0"; } }
if(words[i]!="0") System.out.print("S1 "+words[i]+" "+wrc+"/"+words.length+" "); double irc = wrc; double lel = words.length; freq[i] = (wrc/lel)*Math.log(1/lel); System.out.println(freq[i]); wrc=1; urc=urc-1; if(word[i]!="0") if(urc==0) { System.out.println("S2 "+word[i]+"= 0"); } else {System.out.print("S2 "+words[i]+" "+urc+"/"+word.length+" "); double erc = urc; freqc[i] = (erc/lel)*Math.log(1/lel); System.out.println(freqc[i]);} urc=1;
} // TODO code application logic here } } OUTPUT
SCHOOL OF ADVANCED SCIENCES Winter Semester 2018-19 Lab Assessment – IX Name: Gopal Gupta Faculty: Prof. Thippa Reddy Course: Java Programming
Registration No: 16BIT0276 Slot: L41 + L42 Code: CSE1007
CODE: Index.html <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial scale=1.0"> 1
Welcome.java import java.io.*; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*;
@WebServlet(urlPatterns = {"/welcome"}) public class welcome extends HttpServlet{ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n = request.getParameter("a"); 2
String q = request.getParameter("b"); Cookie c = new Cookie("Name", n); Cookie c1 = new Cookie("College", q); response.addCookie(c); response.addCookie(c1); out.print("Hi "+n+" from "+q+"Campus"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("