Gujarat Technological University MCA – SEM III Programming Skills – JAVA December 2011 Set-1 Create a Fruit class would provide the base for several subclass fruit types such as apples, oranges, pears, etc. Include methods in fruit that provide properties of the fruit such as the color(e.g. String gecolor()), shape, average weight, etc.) The subclasses will override these methods to provide the properties unique to a particular fruit. For one of the classes, e.g pear, create subclasses for particular types, such as Bartlet and Anjou pears. In the main() routine, create instances of the different classes and invoke methods that print out the property values such as color, shape, etc.. Set-2 Write a program in java for restaurant. Create two threads one for placing order and other for generating bills. When customers place an order then and only then Manager can take the Order or generate a bill of an Order. Use Inter Thread Communication. And display the customer order after it place the order. You have to display minimum three order of the customer. import java.io.*; class customerOrder { boolean valueset=false; String str[]=new String[3]; synchronized void d_takeOrder(Thread t) { if(valueset) { try { wait(); }catch(InterruptedException e) { System.out.println(e); } } System.out.println("\n"+t); try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); for(int i=0;i<3;i++) { System.out.print("\n Take an Order "+(i+1)+" :: "); str[i]=br.readLine(); } }catch(IOException e) { System.out.println(e); } valueset=true; notify(); } synchronized void d_dispOrder(Thread t) { if(!valueset) { try { wait(); }catch(InterruptedException e) { System.out.println(e); } } System.out.println("\n"+t); for(int i=0;i<3;i++) { System.out.print("\n Place an Order "+(i+1)+" :: "+str[i]); } valueset=false; notify(); } } class takeOrder implements Runnable { customerOrder d; Thread t; takeOrder(customerOrder d) { this.d=d; t=new Thread(this,"Manager take an order"); t.start(); } publicvoid run() { for(int i=0;i<2;i++) { d.d_takeOrder(t); } } } class dispOrder implements Runnable { customerOrder d; Thread t; dispOrder(customerOrder d) { this.d=d; t=new Thread(this,"Manager place an order"); t.start(); } publicvoid run() { for(int i=0;i<2;i++) { d.d_dispOrder(t); } } } class Restaurant { publicstaticvoid main(String args[]) { customerOrder d=new customerOrder(); new takeOrder(d); new dispOrder(d); } } Set-3 Write a program that demonstrates handling of exceptions in inheritance tree. For example, create base class called Father and derived class called Son which extends the base class. In Father class implement a constructor which takes the age and throws the exception Wrongage() when the input age <0 . In Son class implement a constructor that takes both Father and Son’s age and throws an exception if Son’s age is >= Father’s age. class WrongAge extends Exception { public String toString() { return "Please enter the right age:"; } } class Father { int age; Father(int age1) { age=age1; System.out.println("Father age:"+age); } } class Son extends Father { Son(int age1) { super(age1); System.out.println("Son age:"+age); } } class Set3 { public static void main(String args[]) throws WrongAge { int i=args.length; int j=Integer.parseInt(args[0]); int k=Integer.parseInt(args[1]); if(i<=0 || j<=k) { throw new WrongAge(); } else { Father f=new Father(j); Son s=new Son(k); } } } Set-4 Write a program that sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order. Find out the similar words from the string as well as display the counter of that word. Display the sorted list of the words. Also display no of white space, no of Capital characters and number of vowels in the text. Replace the white space with “/” and display the string in the reverse order. And also implement the following functions. a. charDelete – delete the char from the string b. delete(int s,int e)- return the string after deleting the characters from s to e. Set-5 Create a class Student. Write a student manager program to manipulate the student information from files by the FileInputStream and FileOurputStream. Redefine the student manager program to manipulate the student information from files by using ObjectSerializtion (ObjectInputStream and ObjectOutputStream). Set-6 Write a GUI application in java, which enter the details of a student and on the submit display the details of the student. (Student details is like bio-data, it contains name, address, phone, educational details, project details etc. ) Create three buttons in the applet like new, submit, and view. When the user click the submit button it stores the information of the student in the file. If the students press the new button it allows entering information of the new student. When view button pressed it show all the details of a student. import java.awt.*; import java.awt.event.*; import java.io.*; public class Project9 extends Frame { TextField textfield1; Button button; TextArea textarea = new TextArea("",20,20,20); File outFile = new File("TextField.txt");; FileOutputStream outFileStream = new FileOutputStream(outFile); PrintWriter outStream = new PrintWriter(outFileStream); /*File inFile = new File("TextField.txt"); FileReader myFileReader = new FileReader(inFile); BufferedReader myBufReader = new BufferedReader(myFileReader);*/ public Project9() throws IOException { setTitle("Project9"); setSize(400,400); setLayout(new FlowLayout()); textfield1 = new TextField("",10); button = new Button("Click"); add(textfield1); add(button); add(textarea); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { outStream.write(textfield1.getText()); try { File inFile = new File("TextField.txt"); FileReader myFileReader = new FileReader(inFile); BufferedReader myBufReader = new BufferedReader(myFileReader); //String s; //while((s=myBufReader.readLine())!=null) //{ textarea.append(myBufReader.readLine()); //} myBufReader.close(); } catch(Exception e1) { e1.printStackTrace(); } outStream.close(); } }); } public static void main(String[] args) throws IOException { Project9 guiInterface = new Project9(); guiInterface.setVisible(true); } } OR import javax.swing.*; import java.awt.*; import java.io.IOException; import java.io.InputStreamReader; public class PopulateTextAreaFromFile extends JFrame { public PopulateTextAreaFromFile() { initialize(); } private void initialize() { setSize(300, 300); setTitle("Populate JTextArea from File"); setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); try { // // Read some text from the resource file to display in // the JTextArea. // textArea.read(new InputStreamReader( getClass().getResourceAsStream("TextField.txt")), null); } catch (IOException e) { e.printStackTrace(); } getContentPane().add(scrollPane, BorderLayout.CENTER); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new PopulateTextAreaFromFile().setVisible(true); } }); } } Set-7 Write a program to implement an applet with two buttons and a message as shown below: Do you know Swimming? YES NO If the user clicks YES button . The message “Congratulations. You are selected” is to be displayed. When user clicks NO button the message “Regrets. We are not in a position to select you” is to be displayed. import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; /* <applet code="Panel1" width=200 height=200> </applet> */ public class Panel1 extends Applet implements ActionListener { Button b1; public void init() { setLayout(new BorderLayout()); Panel pn=new Panel(); Label label=new Label("Do you know swimming"); pn.add(label); add(pn,"North"); Panel pc=new Panel(); pc.setLayout(new GridLayout(1,2)); b1=new Button("Yes"); b1.addActionListener(this); pc.add(b1); Button b2=new Button("No"); b2.addActionListener(this); pc.add(b2); add(pc,"Center"); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { JOptionPane.showMessageDialog(null,"Congratulations,u r selected"); } else { JOptionPane.showMessageDialog(null," Regrets. We are not in a position to select you "); } } } Set-8 1. Write a program using map which stores faculty detail (faculty name, Qualification, experience) and retrieve data by entering faculty name 2. Write a program by entering birth date it will display zodiac sign (Hint :Aries March 21 - April 20 ,Taurus - April 21 - May 21 ,Gemini - May 22 - June 21 ,Cancer - June 22 July 22 ,Leo - July 23 -August 21 ,Virgo - August 22 - September 23 ,Libra - September 24 October 23 ,Scorpio - October 24 - November 22 ,Sagittarius - November 23 - December 22 ,Capricorn - December 23 - January 20 ,Aquarius - January 21 - February 19 ,Pisces - February 20- March 20 ) Set-9 Write a program to create a window as a grid of the number buttons from 1 to 15. Set-10 Define one class A in package apack. In class A, three variables are defined of access modifiers protected, private & public. Define class B in package bpack which extends A and write display() method which access variables of class A. Define class C in package cpack which has one method display() in that create one object of class A and display its variables. Define class ProtectedDemo in package dpack in which write main() method. Create objects of class B and C and class display method for both these objects. Set-11 To copy file using character stream. Provide source file name and destination file name using commandline arguments. /* Copy a text file. To use this program, specify the name of the source file and the destination file. For example, to copy a file called FIRST.TXT to a file called SECOND.TXT, use the following command line.java CopyFile FIRST.TXT SECOND.TXT */ import java.io.*; class CopyFile { public static void main(String args[]) throws IOException { int i; FileInputStream fin; FileOutputStream fout; try { // open input file try { fin = new FileInputStream(args[0]); } catch(FileNotFoundException e) { System.out.println("Input File Not Found"); //return; } // open output file try { fout = new FileOutputStream(args[1]); } catch(FileNotFoundException e) { System.out.println("Error Opening Output File"); return; } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Usage: CopyFile From To"); return; } // Copy File try { do { i = fin.read(); if(i != -1) fout.write(i); } while(i != -1); } catch(IOException e) { System.out.println("File Error"); } fin.close(); fout.close(); } } Set-12 Write a program which write 10 numbers in the file. Read this file and identify odd and even numbers among them. import java.io.*; import java.util.*; class OddEven { public static void main(String[] args) throws Exception { //Clear the contents of numbers.dat if already exists and populated File fileOne = new File("test1.txt"); fileOne.delete(); File newFile = new File("test1.txt"); newFile.createNewFile(); //Create writer object FileOutputStream fout=new FileOutputStream(newFile); BufferedOutputStream bos=new BufferedOutputStream (fout); //PrintWriter writer = new PrintWriter(new FileWriter("test1.txt")); //Loop from 1 to 100 for (int i = 1; i <= 100; i++) { //writer.print(i + ","); fout.write(i+" "); } //Close writer object. // writer.close(); //Read from file FileInputStream fin = new FileInputStream("test1.txt"); //DataInputStream in = new DataInputStream(fileStream); //BufferedReader reader = new BufferedReader(fileStream); //Print line int s; while((s=fin.read())!=-1) { if(s%2==1) { System.out.println(s); } } //reader.close(); } } OR import java.io.*; class Writer1 { public static void main(String args[]) { try { FileWriter fw=new FileWriter(args[0]); BufferedWriter bw=new BufferedWriter(fw); for(int i=0;i<12;i++) { bw.write(i+" "); } bw.close(); } catch(Exception e) { System.out.println("Exception: "+e); } try { FileReader fr=new FileReader(args[0]); BufferedReader br=new BufferedReader(fr); int s; System.out.print("even number:"); while((s=br.read())!=-1) { //int a=Integer.parseInt(s); if(s%2==0) { System.out.print((char)s+" "); } } fr.close(); } catch(Exception e) { System.out.println("Exception: "+e); } } } OR import java.io.*; import java.util.*; public class TextFileIO { public static void main(String[] args) throws Exception { //Clear the contents of numbers.dat if already exists and populated File fileOne = new File("numbers.dat"); fileOne.delete(); File newFile = new File("numbers.dat"); newFile.createNewFile(); //Create writer object PrintWriter writer = new PrintWriter(new FileWriter("numbers.dat")); //Loop from 1 to 100 for (int i = 1; i <= 100; i++) { //If number is even, write to file if (i % 2 == 0) { writer.print(i + ","); } } //Close writer object. writer.close(); //Read from file FileInputStream fileStream = new FileInputStream("numbers.dat"); DataInputStream in = new DataInputStream(fileStream); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); //Print line System.out.println(reader.read()); reader.close(); //Write all odd numbers to file PrintWriter writer2 = new PrintWriter(new FileWriter("numbers.dat")); //Loop from 1 to 100 for (int i = 1; i <= 100; i++) { //If number is odd, write to file if (i % 2 == 1) { writer2.print(i + ","); } } writer2.close(); //Read from file FileInputStream fileStream2 = new FileInputStream("numbers.dat"); DataInputStream in2 = new DataInputStream(fileStream2); BufferedReader reader2 = new BufferedReader(new InputStreamReader(in2)); //Print line System.out.println(reader2.read()); reader.close(); } }