Java Programming: Mid Term Test
Java Programming: Mid Term Test
Java Programming: Mid Term Test
Code:
import java.util.Scanner;
package act6qq1;
public class Act6qq1 {
public static void main(String[] args) {
Scanner y = new Scanner(System.in);
System.out.println("Enter your choice:");
int ch=y.nextInt();
switch(ch)
{
case 1:
{
try{
System.out.print("num1:");
int num1 = y.nextInt();
System.out.print("num2:");
int num2 = y.nextInt();
int output = num1/num2;
System.out.println("Result:"+output);
}
catch(ArithmeticException e){
System.out.println("A number shouldn't be divided by zero.");
}
break;
}
case 2:
{
try{
int a[]=new int[10];
a[11]=9;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds");
}
break;
}
case 3:
{
try{
String str=null;
System.out.println(str.length());
}
catch(NullPointerException e){
System.out.println("NullPointerException");
}
break;
}
default:
System.out.println("wrong choice entered");
System.out.println("Mounika 20BCE2963");
}
}
}
Output:
Code:
import java.util.Scanner;
import java.util.InputMismatchException;
package act6q2;
class StudentManagement extends Exception{
StudentManagement(String error){
super(error);
}
}
public class Act6q2 {
public static void main(String[] args) {
try{
Scanner c = new Scanner(System.in);
System.out.println("Enter marks here:");
int h = c.nextInt();
if(!(h>=0 && h<=100)){
throw(new StudentManagement("Invalid Marks:"+h));
}
System.out.print("Entered marks are:\n"+h);
}
catch(InputMismatchException e){
System.out.println("Invalid input..pls enter integer only");
}
catch(StudentManagement e){
System.out.println("Error:"+e);
System.out.println("Mounika 20BCE2963");
}
}
}
Output:
Code:
import java.io.*;
import java.util.Scanner;
class Exception{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
void msg() throws IOException{
if(a<=0){
System.out.println("Input cannot be nehative or zero");
}
else if(a>50 && a<100){
System.out.println("Input cannot be in the range 50 to 100");
}
}
}
class JavaApplication45 {
public static void main(String[] args) throws IOException{
try{
Exception o1=new Exception();
o1.msg();
}
catch(ArithmeticException e){
System.out.println("Exception handled");
}
finally{
System.out.println("Finally block is executed");
System.out.println("Mounika 20BCE2963");
}
}
}
Output:
Code:
import java.util.Scanner;
package act6q4;
class UsernameException extends Exception{
public UsernameException(String msg){
super(msg);
}
}
class PasswordException(String msg){
public class Act6q4 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String username, password;
int length = username.length();
try{
if(length<8)
throw new UsernameException("Username must be greater than 8 characters");
else
System.out.println("Username correct");
}
catch(UsernameException u){
u.printStackTrace();
}
catch(PasswordException p){
p.printStacktrace();
}
finally{
System.out.println("The final statement is executed");
System.out.println("Mounika 20BCE2963");
}
}
}
}
ACTIVITY – 7
Code:
package act7q1;
class OddThread extends Thread{
int limit;
sharedPrinter printer;
public OddThread(int limit,sharedPrinter printer){
this.limit=limit;
this.printer=printer;
}
public void run(){
int oddnumber=1;
while(oddnumber<=limit){
printer.printOdd(oddnumber);
oddnumber=oddnumber+2;
}
}
}
class EvenThread extends Thread{
int limit;
sharedPrinter printer;
public EvenThread(int limit,sharedPrinter printer){
this.limit=limit;
this.printer=printer;
}
public void run(){
int evenNumber=2;
while(evenNumber<=limit){
printer.printEven(evenNumber);
evenNumber =evenNumber+2;
}
}
}
class sharedPrinter{
boolean isOddPrinted = false;
synchronized void printOdd(int number){
while(isOddPrinted){
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+number);
isOddPrinted=true;
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
notify();
}
synchronized void printEven(int number){
while(!isOddPrinted)
{
try{
wait();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+number);
isOddPrinted=false;
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
notify();
}
}
public class Act7q1 {
public static void main(String args[]){
sharedPrinter printer = new sharedPrinter();
OddThread oddThread = new OddThread(10, printer);
oddThread.setName("Odd-Thread");
EvenThread evenThread = new EvenThread(10, printer);
evenThread.setName("Even-Thread");
oddThread.start();
evenThread.start();
}
}
Output:
Code:
import java.util.*;
package act7q2;
class q20{
ArrayList<Integer> factorList; ArrayList<Integer> primeList;
public q20() {
this.factorList = new ArrayList<>(); this.primeList = new ArrayList<>();
}
private int sumOfDigits(int n) {
int sum = 0; while (n > 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
private boolean isPrime(int n) {
int found = 0;
if (n == 1) { return false;
}
if (n == 2) { return true;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
{
found = 1;
break;
}
}
if (found == 1) { return false;
}
return true;
}
synchronized void findFactorSumNumber() {
for (int i = 100; i <= 200; i++) {
if (i % sumOfDigits(i) == 0) { System.out.print(i + " "); factorList.add(i);
}
if (i % 10 == 0) {
try {
System.out.println(Thread.currentThread().getName() + " notifying.");
notifyAll(); wait(2000);
} catch (Exception e) { e.printStackTrace();
}
}
}
}
synchronized void findPrimeSumNumber() {
for (int i = 0; i <= 100; i++) {
if (isPrime(sumOfDigits(i))){ System.out.print(i + " "); primeList.add(i);
}
if (i % 10 == 0) {
try {
System.out.println(Thread.currentThread().getName() + " notifying.");
notifyAll(); wait(2000);
} catch (Exception e) { e.printStackTrace();
}
}
}
}
}
class Act7q2 {
public static void main(String[] args) {
q20 op = new q20();
Thread t1 = new Thread(){
public void run(){
op.findFactorSumNumber();
}
};
Thread t2 = new Thread(){
public void run(){
op.findPrimeSumNumber();
}
};
t1.start();
t2.start();
try{
t1.join();
t2.join();
System.out.println("Factor Sum Number List:"+op.factorList);
System.out.println("Prime SumNumber List:"+op.primeList);
System.out.println("Mounika 20BCE2963");
}
catch(Exception e){
e.printStackTrace();
}
}
}
Output: