BSCCS2005: Practice Programming Assignment Solutions
Week {2-6}
1. Week 2 PPA1
import [Link].*;
class Rectangle{
int w; //width
int h; //height
public void setw(int _w) {
w = _w;
}
public void seth(int _h) {
h = _h;
}
public int area() {
return w * h;
}
}
public class FClass{
public static void main(String[] args){
Scanner sc= new Scanner([Link]);
int w = [Link]([Link]());
int h = [Link]([Link]());
Rectangle r = new Rectangle();
[Link](w);
[Link](h);
int area = [Link]();
[Link](area);
}
}
2. PPA2
import [Link].*;
class FClass {
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
String s1 = [Link]();
evenDisplay(s1);
}
public static void evenDisplay(String str1){
String str3 = "";
for (int i = 0; i < [Link](); i++) {
if(i % 2 == 0) {
str3 = str3 + [Link](i);
}
}
[Link](str3);
}
}
Page 2
3. Week 3 PPA1
import [Link].*;
class Calculator{
public void sum(double a, double b){
[Link](a+b);
}
public void subtraction(double a, double b){
[Link](a-b);
}
public void multiply(double a, double b){
[Link](a*b);
}
public void division(double a, double b){
[Link](a/b);
}
}
class UpdatedCalculator extends Calculator{
public void remainder(double a, double b){
[Link](a%b);
}
}
public class CalculatorCheck{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double n1 = [Link]();
double n2 = [Link]();
Calculator c = new Calculator();
[Link](n1, n2);
[Link](n1, n2);
[Link](n1, n2);
[Link](n1, n2);
UpdatedCalculator uc = new UpdatedCalculator();
[Link](n1, n2);
}
Page 3
4. PPA2
import [Link].*;
class Point{
private int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
public String toString() {
return "("+x+", "+y+")";
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
Point other = (Point) obj;
return x == other.x && y == other.y;
}
}
class FClass{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int x1 = [Link]();
int y1 = [Link]();
int x2 = [Link]();
int y2 = [Link]();
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
if([Link](p2))
[Link](p1 + "==" + p2);
else
[Link](p1 + "!=" + p2);
}
}
Page 4
5. Week 4 PPA1
import [Link].*;
interface Searchable{
public int search(int start_index, Object key);
}
class Char{
private char c;
public Char(char c_) {
c = c_;
}
public boolean equals(Object d) {
if (d instanceof Char) {
Char otherChar = (Char) d;
return this.c == otherChar.c;
}
return false;
}
}
class CharArray implements Searchable{
private Char[] carr;
public CharArray(Char[] carr_){
carr = carr_;
}
public int search(int start_index, Object key) {
if (key instanceof Char) {
Char searchKey = (Char) key;
for (int i = start_index; i < [Link]; i++) {
if (carr[i].equals(searchKey)) {
return i;
}
}
}
return -1;
}
}
class FrequencyCounter{
public static int getFrequency(Searchable ob, Object key){
if (ob instanceof CharArray) {
if (ob instanceof CharArray) {
int frequency = 0;
int index = 0;
while (true) {
Page 5
index = [Link](index, key);
if (index == -1) {
break;
}
frequency++;
index++;
}
return frequency;
}
}
return 0;
}
}
class FClass{
public static void main(String[] args) {
String str;
char c;
Scanner sc = new Scanner([Link]);
str = [Link]();
c = [Link]().charAt(0);
Char key = new Char(c);
Char[] cA = new Char[[Link]()];
for(int i = 0; i < [Link](); i++) {
cA[i] = new Char([Link](i));
}
CharArray caObj = new CharArray(cA);
[Link]([Link](caObj, key));
}
}
Page 6
6. PPA2
import [Link];
class Voter{
public static int total_no_of_voters;
private static Voter new_voter;
private static int current_voter_count = 0;
private Voter() {
current_voter_count++;
}
public static Voter getVoter() {
if(new_voter == null) {
new_voter = new Voter();
return new_voter;
}
else {
return null;
}
}
public void firstVoter(){
if(new_voter != null) {
EVM new_machine = [Link](new_voter);
new_machine.startVoting();
}
}
public void callNewVoter() {
new_voter = null;
[Link]("Voting completed for voter " +
current_voter_count);
// Calling new voter and allocating EVM for him
if(current_voter_count < total_no_of_voters) {
//EVM - Voter mapping is bijective
EVM new_machine = [Link](getVoter());
try {
EVM x = [Link](null);
[Link]();
Page 7
} catch(NullPointerException e) {
[Link]("EVM is Singleton");
}
new_machine.startVoting();
}
}
}
class EVM{
private Voter current_voter;
private static int evm_count = 0;
private static EVM current_evm;
private EVM(Voter v) {
current_voter = v;
evm_count++;
}
public static EVM getEVM(Voter v) {
if(current_evm == null) {
current_evm = new EVM(v);
return current_evm;
//this particular evm is binded to the current voter
}
else
return null;
}
public void startVoting() {
[Link]("voting under process on EVM number "+ evm_count);
current_evm = null; //voting on current evm is done
current_voter.callNewVoter();
}
}
public class Election{
public static void main(String args[]) {
Scanner s = new Scanner([Link]);
Voter.total_no_of_voters = [Link]();
// Assume input is always non zero
Voter v = [Link]();
//Trying to create another voter when one voter is in the middle of
//voting process, students can ignore this try-catch block of code
Page 8
try {
Voter x = [Link]();
[Link]();
} catch(NullPointerException e) {
[Link]("Voter is Singleton");
}
//Starting the first vote of the day
[Link]();
}
}
Page 9
7. Week 5 PPA1
import [Link].*;
import [Link].*;
class ClassStats{
public static int getPubMethodCount(String cname) {
try {
Class<?> clazz = [Link](cname);
Method[] methods = [Link]();
return [Link];
}catch(Exception e) { return -1; }
}
public static int getAllMethodCount(String cname) {
try {
Class<?> clazz = [Link](cname);
Method[] methods = [Link]();
return [Link];
}catch(Exception e) { return -1; }
}
public static int getPubFieldCount(String cname) {
try {
Class<?> clazz = [Link](cname);
Field[] fields = [Link]();
return [Link];
}catch(Exception e) { return -1; }
}
public static int getAllFieldCount(String cname) {
try {
Class<?> clazz = [Link](cname);
Field[] fields = [Link]();
return [Link];
}catch(Exception e) { return -1; }
}
public static int getPubContCount(String cname) {
try {
Class<?> clazz = [Link](cname);
Constructor<?>[] constructors = [Link]();
return [Link];
}catch(Exception e) { return -1; }
}
public static int getAllContCount(String cname) {
try {
Class<?> clazz = [Link](cname);
Page 10
Constructor<?>[] constructors = [Link]();
return [Link];
}catch(Exception e) { return -1; }
}
}
class FClass{
public static void main(String[] args) {
String cname;
Scanner sc = new Scanner([Link]);
cname = [Link]();
[Link]("Constructor: " +
[Link](cname) + ", " +
[Link](cname));
[Link]("Fields: " +
[Link](cname) + ", " +
[Link](cname));
[Link]("Methods: " +
[Link](cname) + ", " +
[Link](cname));
}
}
Page 11
8. PPA2
import [Link];
class ConvertArrays{
public Double doubleArr[]=new Double[3];
public Integer intArr[]=new Integer[3];
public int x=0,y=0,z=0;
public void convert(String[] arr){
for (int i = 0; i < [Link]; i++) {
if(arr[i].contains("."))
{
doubleArr[y]=[Link](arr[i]);
y++;
}
else
{
intArr[z]=[Link](arr[i]);
z++;
}
}
}
public <T> void display(T[] arr){
for(T elements:arr)
[Link](elements+" ");
[Link]();
}
}
public class Programming {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
String arr[]= new String[6];
for (int i = 0; i < [Link]; i++) {
arr[i]=[Link]();
}
ConvertArrays conArrays=new ConvertArrays();
[Link](arr);
[Link]("===After conversion Arrays===");
[Link]([Link]);
[Link]([Link]);
}
}
Page 12
9. Week 6 PPA1
import [Link].*;
class RemoveStudent{
public boolean property(Double value) {
if(value<65)
return true;
return false;
}
public void detained(Map<String, Double> obj) {
Collection<Double> values=[Link]();
Iterator<Double> iterator=[Link]();
while ([Link]()) {
if(property([Link]()))
[Link]();
}
display(obj);
}
public void display(Map<String, Double> obj) {
[Link](obj);
}
}
public class Test {
public static void main(String[] args) {
Map<String,Double> map=new TreeMap<String,Double>();
Scanner scanner=new Scanner([Link]);
for (int i=0; i<6; i++) {
[Link]([Link](),[Link]());
}
RemoveStudent obj=new RemoveStudent();
[Link](map);
}
}
Page 13
10. PPA2
import [Link].*;
abstract class Account implements Comparable<Account>{
String acc_no;
double balance;
public Account(String no,double bal){
acc_no = no;
balance = bal;
}
public int compareTo(Account obj) {
if([Link] > [Link])
return 1;
else if([Link] < [Link])
return -1;
else
return 1;
}
public boolean equals(Object o) {
Account obj = (Account)o;
return this.acc_no.equals(obj.acc_no);
}
public int hashCode() {
return [Link](acc_no);
}
}
class SavingsAccount extends Account{
public SavingsAccount(String acc_no, double bal) {
super(acc_no,bal);
}
public String toString() {
return "Savings Account:"+acc_no+" , Balance:"+balance;
}
}
class CurrentAccount extends Account{
double overdraft_limit;
public CurrentAccount(String acc_no, double bal, double odl) {
super(acc_no,bal);
overdraft_limit = odl;
}
public String toString() {
return "Current Account:"+acc_no+" , Balance:"+balance;
}
}
Page 14
public class Test4 {
public static void accountProcessor(ArrayList<Account> unprocessed_acc) {
Set<Account> unique_accounts;
unique_accounts = new LinkedHashSet<Account>(unprocessed_acc);
Set<Account> sorted_accounts = new TreeSet<Account>(unique_accounts);
for(Account a : sorted_accounts) {
[Link](a);
}
}
public static void main(String args[]) {
Scanner s = new Scanner([Link]);
ArrayList<Account> acc = new ArrayList<Account>();
int s_acc_count = [Link]();
for(int i=1;i<=s_acc_count;i++) {
String no = [Link]();
double bal = [Link]();
[Link](new SavingsAccount(no,bal));
}
int c_acc_count = [Link]();
for(int i=1;i<=c_acc_count;i++) {
String no = [Link]();
double bal = [Link]();
double lim = [Link]();
[Link](new CurrentAccount(no,bal,lim));
}
accountProcessor(acc);
}
}
Page 15