You are on page 1of 5

Syed Abdul Rafay 2112239

Task 1:
Main:
package Task1;

import java.util.Scanner;

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
VoterId id = new VoterId();
int tmpage;
System.out.println("Enter the age of Voter:");
tmpage = sc.nextInt();
try{
id.vote(tmpage);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

Voter Id:
package Task1;

public class VoterId {


private int age;

public void setAge(int age) {


this.age = age;
}

public int getAge() {


return age;
}
public void vote(int age)throws Exception{
setAge(age);
if (age>=18){
System.out.println("Voter can cast Vote.");
}
else{
Exception ex = new Exception("Voter is not eligible.");
throw ex;
}
}
}

Output:
Task 2:
Account:
package Task2;

import java.util.Scanner;

public class Account


{
public static void main(String[] args)
{
int amount;
Scanner sc = new Scanner(System.in);
System.out.println("\"Account Opening\"\nEnter the amount you want to Open your
Account with: ");
amount = sc.nextInt();
if (amount<=0)
{
throw new ArithmeticException("Insufficient amount");
}
else
{
System.out.println("Your New Account Balance : "+amount);
}
}
}
Output:

Task 3:
Main:
package Task3;

public class Main


{
public static void main(String[] args) throws Exception {
Array arr = new Array();
try
{
System.out.println("Sixth value of your array : ");
arr.returnarr(arr.numarray);
}catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("\nSixth value does not exist for your array");
}
}
}

Array:
package Task3;
public class Array
{
int numarray[]={5,4,3,2,1};

public void setNumarray(int[] numarray)


{
this.numarray = numarray;
}

public int returnarr(int[] numarray)


{
return numarray[6];
}
}

You might also like