HUSNAIN KAZMI
F17-9427, B
LAB 03
TASK 02
REGULAR EXPRESSION IN JAVA
package task_02;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Declare the class for task 01
public class regex {
// Variable defining here
String student_name;
String student_rollNo;
int age;
Scanner input = new Scanner(System.in);
// Now, constructor declaring(I prefer it with parameters) here
public regex(String student_name, String student_rollNo)
{
this.student_name = student_name;
this.student_rollNo = student_rollNo;
}
// Now, I'm defining the Methods which are required
public void check_rollNo()
{
System.out.print("Enter your Roll No with specific pattern, otherwise
system will not allow to save your No.:");
String roll = input.next();
//Checking with the RE
Pattern pattern = Pattern.compile("[0-19]\\]-[0000,9999]"); // F..-....
Matcher m = pattern.matcher(roll);
if(m.find()!=true)
{
System.out.println("Sorry, You Entered your Roll.NO incorrect!!!
try again.");
}
// System.out.println("You entered your name:" + roll);
}
regex rule = new regex("Kazmi","66666" );
rule.check_rollNo();
// System.out.println("Yahoooo, Working Thanks Allah " );
// System.out.println(tuffy.toString());
}
Working Screenshot, Roll NO is incorrect
Working Screenshot, Roll NO is Correct
TASK 03
BANK ACCOUNT, AND TYPE OF BANK ACCOUNT
package main;
import java.util.Scanner;
public abstract class Accounts {
// Instance variables
String name;
int balance, accountType;
Scanner input = new Scanner(System.in);
// Methods that are required for all Accounts
abstract void serviceFee();
abstract void interest();
abstract void checkBalance();
public void showBalance() {
System.out.println("Your current balance is:" + balance + "\n");
public void deposite()
{
System.out.println("Enter amount to be deposited: \n");
int newBalance = input.nextInt();
balance += newBalance; // add balance
System.out.println("You have deposited " + newBalance + "
successfully\nYour new Balance is: " + balance);
System.out.println("Thank you for HBL");
}
public void createAccount()
{
System.out.println("Enter your Name: \n");
String name = input.next();
// System.out.println("Enter Account Type: \n1. Saving\n2. Checking\n");
// int accountType = input.nextInt();
balance = 0;
System.out.println("Your current Balance is:" + balance + "\n");
System.out.println("Your account created successfully\n");
}
public void control()
{
System.out.println("1. Create Account\n2. Check Balance\n3. Deposite
Money\n4. Checking Account\n");
int choice = input.nextInt();
switch(choice)
{
case 1:
createAccount();
control();
break;
case 2:
showBalance();
control();
case 3:
deposite();
control();
default:
System.out.println("Program is going to turn off!!!\n");
System.exit(0);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Accounts obj = new SavingAccount();
obj.control();
// Accounts savingAccount = new SavingAccount();
// savingAccount.createAccount();
// savingAccount.checkBalance();
// Accounts checkingAccount = new CheckingAccount();
// checkingAccount.checkBalance();
//
class SavingAccount extends Accounts
{
public void serviceFee()
{
System.out.println("You are NOT charged for Service Free\n");
}
public void interest()
{
System.out.println("You are required to give interest in this type of
Account!!!");
}
public void checkBalance()
{
System.out.println("You don't have permission to check Balance: \n");
}
}
class CheckingAccount extends Accounts
{
public void serviceFee()
{
System.out.println("You are charged for Service Free\n");
}
public void interest()
{
System.out.println("Interest is not allowed in this type of
Account!!!");
}
public void checkBalance()
{
System.out.println("Your current balance is:" + balance + "\n");
}
}
Task Unk07
Inheritance Ship Class
package main;
public class Ship {
// Instance Variables
String shipName, yearBuilt;
//
// Both Constructors override and default
public Ship()
{
// Default Constructor
shipName = "Pak Ship";
yearBuilt = "1990-08-01";
}
Ship(String name, String date)
{
name = shipName;
date = yearBuilt;
}
// Methods
public void showDetail() {
System.out.println("Ship Name:" + shipName );
System.out.println("\nBuilt date of ship: " + yearBuilt);
}
public static void main(String[] args)
{
CruiseShip obj = new CruiseShip();
Ship obj2 = new Ship();
obj.showDetail();
System.out.println("Now Ship class\n");
obj2.showDetail();
}
}
// If there is default Constructor in Super class, and not in subclass, NO Error. But
you should define here construstor also
class CruiseShip extends Ship
{
// Instance Variable
int maxPassenger;
// Constructor
public CruiseShip()
{
maxPassenger = 500;
shipName = "Cruise Ship";
yearBuilt = "2009-08-01";
// Methods
public void showDetail()
{
System.out.println("\nShip Name:" + shipName );
// System.out.println("\nBuilt date of ship: " + yearBuilt);
System.out.println("\nMaximum NO. of Passengers: " + maxPassenger);
}
}
class CargoShip extends Ship
{
// Instance Variable
int cargoCapacity ;
// Constructor
public CargoShip()
{
cargoCapacity = 55;
shipName = "Cargo Ship";
yearBuilt = "2019-08-01";
}
// Method
public void showDetail()
{
System.out.println("\nShip Name:" + shipName );
System.out.println("\nShip Capacity :" + cargoCapacity);
}
}