You are on page 1of 7

Exercise 1: Basics and Packages 1.

Write a program to receive a string from command line and check the presence of vowels (a, e, i, o, u) and print vowel and number of occurrences of vowels in the string. (Reading from command line) For example if the input string is accommodation Output should be: a-2 e-0 i-1 o-2 u-0 import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.StringTokenizer; public class FindWord { public static void main(String args[]) { int c1,c2,c3,c4,c5,n,ch; String input=new String(args[0]); c1=0; c2=0; c3=0; c4=0; c5=0; try{ n=input.length(); for(int i=0;i<n;i++) { ch=input.charAt(i); if (ch== 65|| ch==97) { c1++; } else if(ch==69 ||ch== 101) { c2++; } else if(ch==73 || ch==105) { c3++; } else if(ch==79 || ch== 111) {

c4++; } else if(ch==85 || ch==117) { c5++; } } System.out.println("a- "+c1 ); System.out.println("e- "+c2 ); System.out.println("i- "+c3 ); System.out.println("o- "+c4 ); System.out.println("u- "+c5 ); } catch(Exception ex) { ex.printStackTrace(); } } } 2. Write an interface with a method named add which is implemented by two classes IntClass and StrClass. IntClass has two int variables and StrClass has two String variables. When add of IntClass is called it should print sum of two integers whereas when add of StrClass is called it should print concatenated Strings. Interface,IntClass and StrClass should be in one package and Program which uses IntClass and StrClass should be in other package. (Interface & Package) For Input 10, 10 when used in IntClass output should be 20. For Input 10, 10 when used in StrClass output should be1010. package iclass; public interface IAdd { public void add(String s1, String s2); } package iclass; public class IntClass implements IAdd { int a,b,c; public void add(String s1, String s2) { a=Integer.parseInt(s1); b=Integer.parseInt(s2); c=a+b; System.out.println("The addition of the two integer is"+c); }

package iclass; public class StrClass implements IAdd { public void add(String s1, String s2) { System.out.println("The concatenation of two string is"+ s1+s2); } } package IPrint; import iclass.*; import java.io.*; class PrintClass { public static void main(String arg[])throws IOException { IntClass ic=new IntClass(); StrClass sc=new StrClass(); String ar1,ar2; DataInputStream buf=new DataInputStream(System.in); System.out.println("Enter the two numbers:"); ar1=buf.readLine(); ar2=buf.readLine(); ic.add(ar1,ar2); sc.add(ar1,ar2); } }

3. Imagine a petrol vending machine which accepts vehicle type and liter as input. When vehicle type is car maximum petrol value acceptable is 60 and when type is two-wheeler maximum petrol value acceptable is 35.Write a Class with three variable vehicle number, vehicle type and petrol needed. When user enters more than the maximum acceptable values for that type raise an exception. (Exception Handling , Reading from console) For example if Inputs given is TN-23-AB3321, Car, 100 - Raise exception If Inputs given is TN-38-AB9550, TW, 60 Raise exception

import java.lang.Exception; import java.io.*; class MyExp extends Exception { MyExp(String msg) { super(msg); } } class test { public static void main(String args[])throws IOException { String type1=new String("car"); String type2=new String("tw"); int p_value; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter vachile number:"); String type=br.readLine(); System.out.println("enter vachile type:"); String name=br.readLine(); System.out.println("enter value of petrol:"); p_value=Integer.parseInt(br.readLine()); try { if(name.equals(type1)) { if(p_value>=100) { throw new MyExp("invalid petrol value"); } } else if(name.equals(type2)) { if(p_value>=60) { throw new MyExp("invalid petrol value"); } } } catch(MyExp e) { System.out.println(e.getMessage()); }

} } 4. Get an amount(no decimal part) from user and ask him to choose which option he need the change: Option 1: need change in 100s (Give him 100s to the max and rest in 10s, 5s and 1s) Option 2: need change in 50s (Give him maximum number of 50s and rest in 10s, 5s and 1s) Option 3: need change in 10s (Give maximum number of 10s and rest in 5s and 1s) (General) For Example: Input is 1257 Option 1: 100s -12, 10s -5, 5s 1 and 1s-2 Option 2: 50s 25, 5s 1 and 1s-2 import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.*; public class FindNum { public static void main(String args[])throws IOException { int num,choice,t1,t2,t3,t4,temp1,temp2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("please enter integer number:"); num=Integer.parseInt(br.readLine()); System.out.println("Option 1: 100s , 10s , 5s and 1"); System.out.println("Option 2: 50s , 5s and 1s"); System.out.println("Option 3: 10s, 5s and 1s"); System.out.println("Please select ur option for conversation :"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: t1=num/100; temp1=num%100; t2=temp1/10; temp2=temp1%10; t3=temp2/5; t4=temp2%5;

System.out.println("Number of 100s in "+ num+ "\t" +t1); System.out.println("Number of 10s in "+ num+ "\t" +t2); System.out.println("Number of 5 s in "+ num+"\t" +t3); System.out.println("Number of 1s in "+ num+ "\t" +t4); break; case 2: t1=num/50; temp1=num%50; t2=temp1/5; temp2=temp1%5; t3=temp2/1; System.out.println("Number of 50s in " + num+ "\t:\t" +t1); System.out.println("Number of 5s in "+ num+ "\t:\t" +t2); System.out.println("Number of 1s in "+ num+ "\t:\t" +t3); break; case 3: t1=num/10; temp1=num%10; t2=temp1/5; temp2=temp1%5; t3=temp2/1; System.out.println("Number of 10s in "+ num+ "\t:\t" +t1); System.out.println("Number of 5s in "+ num+ "\t:\t" +t2); System.out.println("Number of 1s in "+ num+ "\t:\t" +t3); break; } } }

Exercise 2: Exceptions 1. Here are three packages "objs" holding all objects - circle, square and point, package "inter" holds to interfaces Cbrd which has method to draw circle alone interface CSbrd extended class of Cbrd and one more method to draw square. Next package "app" with class DrawBoard which implements CSbrd and makes it possible to draw circle object as well as square object on the Drawingboard. Note: Literally here no drawing is drawn this is program or drawing board is created will only print the properties of object on draw method. Work to be enhanced: Include an exception to check the size of circle or square and while drawing it and raise an exception for circle when the circle radius*2 is greater than the size of board and circle radius is within the drawboard size and for square check the side is greater than the board side, and the point quard is within the drawboard size.

You might also like