School of Computing s=br.
readLine();
Second CIA Test – March 2019 }
Course Code: BCSCCS404R04/ BITCIT404R04/ BICCIC404R04 System.out.println(count);
f1.close();
Course Name: Java Programming f2.close();
Duration: 90 minutes Max Marks: 50 }
}
PART A 10 x 2 = 20 Marks
Answer all questions. 4. PrintStream and PrintWriter Classes
5. Length=11;Capacity=27; before=W; buffer=Hello io;chartAt9=(6) after=I;
1. 1122334455
2. isAlive( ) method returns true if the thread upon which it is called is still running. Join() 6. 2 5 10 11 12 22
method waits until the thread on which it is called terminates. Its name comes from the
concept of the calling thread waiting until the specified thread joins it. 7. import java.applet.*;
import java.awt.*;
3. import java.io.*; import java.awt.Font;
class Demo16 public class q2 extends Applet
{ {
public static void main(String ar[])throws String msg;
IOException public void init()
{ {
int count=0; msg="SASTRA Deemed to be University ";
String sd="good"; }
FileInputStream f1=new public void paint(Graphics g) {
FileInputStream("e://java/good.txt"); char ch;
FileOutputStream f2=new ch = msg.charAt(0);
FileOutputStream("e://java/good1.txt"); msg = msg.substring(1, msg.length());
BufferedReader br=new BufferedReader(new msg += ch;
InputStreamReader(f1)); g.drawString(msg, 50, 30);
BufferedWriter bw=new BufferedWriter(new try
OutputStreamWriter(f2)); {Thread.sleep(100);}catch(InterruptedException e){}
String s=br.readLine(); repaint();
while(s!=null) }
{ }
int intIndex = sd.indexOf(sd); //<applet code="q2" height=1000 width=1000></applet>
if(intIndex == - 1) 8. import java.applet.*;
count+=1; import java.awt.*;
String import java.math.*;
output=s.substring(0,1).toUpperCase()+s.substring(1); public class Demo6 extends Applet
System.out.println(output); {
bw.write(output); int x1,y1,x2,y2,x11,y11,x12,y12;
bw.newLine(); public void init()
bw.flush(); {
x1=500; ActionListener
y1=400; {
x2=500; Color[] colors={Color.red,Color.blue,Color.green};
y2=300; int i=0;
x11=600; Button bcolor1;
y11=400; Label bcolor;
x12=600; public void init()
y12=300; {
} bcolor = new Label("Select any of the following
public void paint(Graphics g) button to change the background colour");
{ bcolor1 = new Button("Red"); //Declaring
g.setColor(Color.BLUE); buttons
g.drawLine(x1,y1,x2,y2);
g.drawLine(x11,y11,x12,y12); add(bcolor1); //Adding
g.drawLine(x1,y1,x11,y11); buttons to the applet window
for(int i=100;i>0;i--)
{ bcolor1.addActionListener(this); //Adding
g.drawLine(x1,y1,x11,y11); action listener to the button
try }
{ public void actionPerformed(ActionEvent ae)
Thread.sleep(100); {
}catch(InterruptedException e){} if(ae.getSource()==bcolor1)
y1-=1; {
y11-=1; repaint();
if(i==1) }
g.drawString("Tank Full",500,100); i++;
} if(i==3)i=0;
}
} }
//<applet code="Demo6" Height=1000 width=1000></applet> public void paint(Graphics g)
{
9. Delegation Event Model, defines standard and consistent mechanisms to generate and setBackground(colors[i]);
process events. Its concept is quite simple: a source generates an event and sends it to
one or more listeners. }
10. import java.applet.*; }
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.Color;
/*
<applet code="colordemo" height=300 width=300>
</applet>
*/
public class colordemo extends Applet implements
{
if (elective.isHCIavailable())
PART B 3 x 10 = 30 Marks {
elective.chooseElective(e);
System.out.println(t.getName()+"-->
Answer any THREE questions. HCI");
}
11. //DE.java
import java.io.*; else
class DE {
{ System.out.println(t.getName()+"-->
int nHCI=20; Elective not available. Choose again");
int nMCS=20; }
synchronized void chooseElective(int option) }
{ else if (e == 2)
if (option == 1) {
nHCI--; if (elective.isMCSavailable())
else if (option == 2) {
nMCS--; elective.chooseElective(e);
} System.out.println(t.getName()+"-->
synchronized boolean isHCIavailable() MCS");
{ }
return nHCI>0; else
} {
System.out.println(t.getName()+"-->
synchronized boolean isMCSavailable() Elective not available. Choose again");
{ }
return nMCS>0; {
} System.out.println(t.getName()+"-->
} Invalid Option");
//ElectiveThread.java }
class ElectiveThread implements Runnable }
{ }
DE elective; //Student.java
Thread t; public class Student
int e; {
public ElectiveThread(DE d, String name, int String regno;
option){ String name;
elective = d; int option;
e = option; DE d;
t = new Thread(this, name); ElectiveThread et;
t.start();
} public Student(String name, String regno, int
public void run() opt, DE d)
{ {
if (e == 1) this.name = name;
this.regno = regno; 12. package com.jnet.pso;
this.option = opt; import java.awt.*;
this.d = d; import java.awt.event.*;
} import java.applet.*;
public void CBCS() import java.io.*;
{ import java.io.File;
et = new ElectiveThread(d, regno+","+name, public class Demo15 extends Applet implements
option); ActionListener
} {
TextField t1;
public static void main(String args[]) throws TextField t2;
Exception Button b;
{ String name;
DE de = new DE(); public void init()
Student s[] = new Student[40]; {
BufferedReader br = new BufferedReader(new t1=new TextField("",20);
InputStreamReader(System.in)); t2=new TextField(20);
for (int i=0; i<40; i++) b=new Button("Submit");
{ add(t1);
// String rno=""+(int)(Math.random()*10000)+1; add(t2);
// String name =""+rno; add(b);
// int opt = (int)(Math.random()*2)+1; b.addActionListener(this);
System.out.print("Enter Reg. No.:"); t1.addMouseListener(new myadap(this));
String rno = br.readLine(); t2.addMouseListener(new myadap(this));
System.out.print("Enter Name: "); }
String name = br.readLine(); public void actionPerformed(ActionEvent e1)
System.out.print("Enter Option: 1-For HCI {
and 2-For MCS: "); try
int opt = Integer.parseInt(br.readLine()); {
s[i] = new Student(rno, name, opt, de); File f=new File("e:\\prog1.java");
} f.setWritable(true);
for (int i=0; i<40; i++) FileOutputStream outFileStream = new
{ FileOutputStream(f);
s[i].CBCS(); PrintWriter outStream = new
} PrintWriter(outFileStream);
for (int i=0; i<40; i++) outStream.write(t1.getText());
{ outStream.close();
s[i].et.t.join(); }catch(IOException i){}
} }
System.out.println("HCI Remaining: " + class myadap extends MouseAdapter
de.nHCI); {
System.out.println("MCS Remaining: " + Demo15 d;
de.nMCS); myadap(Demo15 d1)
} {
} d=d1;
} Scanner scan = new Scanner(System.in);
public void mouseClicked(MouseEvent e) int n = scan.nextInt();
{
if(e.getSource()==t1) Player player[] = new Player[n];
showStatus("Hello"); Checker checker = new Checker();
if(e.getSource()==t2)
showStatus("Hai"); for(int i=0; i < n; i++){
} player[i] = new Player(scan.next(),
} scan.nextInt());
} }
//<applet code="Demo15" Height=300 scan.close();
Width=300></applet>
13. import java.util.*; Arrays.sort(player, checker);
for(int i = 0; i < player.length; i++){
class Checker implements Comparator { System.out.printf("%s %s\n",
public int compare(Object o1, Object o2) { player[i].name, player[i].score);
Player p1 = (Player) o1; }
Player p2 = (Player) o2; }
}
if (p1.score == p2.score) {
return 14. Explanation for Frames- 3 Marks.
p1.name.compareTo(p2.name); Program -7 marks
} // Create a child frame window from within an
else if (p1.score < p2.score) { applet.
return 1; import java.awt.*;
} import java.awt.event.*;
else { import java.applet.*;
return -1; /*
} <applet code="AppletFrame" width=300 height=50>
} </applet>
*/
} // Create a subclass of Frame.
class Player{ class SampleFrame extends Frame {
String name; SampleFrame(String title) {
int score; super(title);
// create an object to handle window events
Player(String name, int score){ MyWindowAdapter adapter = new
this.name = name; MyWindowAdapter(this);
this.score = score; //register it to receive those events
} addWindowListener(adapter);
} }
public void paint(Graphics g) {
class Solution { g.drawString("This is in frame window", 10, 40);
}
public static void main(String[] args) { }
class MyWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyWindowAdapter(SampleFrame sampleFrame) {
this.sampleFrame = sampleFrame;
}
public void windowClosing(WindowEvent we) {
sampleFrame.setVisible(false);
}
}
// Create frame window.
public class AppletFrame extends Applet {
Frame f;
public void init() {
f = new SampleFrame("A Frame Window");
f.setSize(250, 250);
f.setVisible(true);
}
public void start() {
f.setVisible(true);
}
public void stop() {
f.setVisible(false);
}
public void paint(Graphics g) {
g.drawString("This is in applet window", 10, 20);
}
}