Week 5 GRPA 1
//Add your code for ComplexNum here
class ComplexNum<T extends Number> {
private T r; // Real part
private T i; // Imaginary part
public ComplexNum(T r, T i) {
this.r = r;
this.i = i;
}
public ComplexNum<Double> add(ComplexNum<Double> other) {
double sumReal = r.doubleValue() + other.r.doubleValue();
double sumImaginary = i.doubleValue() + other.i.doubleValue();
return new ComplexNum<Double>( sumReal, sumImaginary);
}
@Override
public String toString() {
return r.doubleValue() + (i.doubleValue() >= 0 ? " + " : " - ") +
Math.abs(i.doubleValue()) + "i";
}
}
Week 5 GRPA 2
// Define constructor
public ArrayExample(T[] a) {
this.a = a;
}
// Define method display() that prints the elements of array a
public void display() {
for (T element : a) {
System.out.print(element + " ");
}
System.out.println();
}
// Define method elementCount(T x) that counts the number of times x is present
in the array a
public int elementCount(T x) {
int count = 0;
for (T element : a) {
if (element.equals(x)) {
count++;
}
}
return count;
}
}
public class ArrayObject {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len = sc.nextInt(); // Taking input for length of the array
Integer[] x = new Integer[len];
for (int i = 0; i < len; i++) {
x[i] = sc.nextInt(); // Taking input for Integer array
}
// Create an object obj for Integer array
ArrayExample<Integer> obj = new ArrayExample<>(x);
int s1 = sc.nextInt(); // Taking input for the value to be counted
String[] y = new String[len];
for (int i = 0; i < len; i++) {
y[i] = sc.next(); // Taking input for String array
}
// Create an object obj1 for String array
ArrayExample<String> obj1 = new ArrayExample<>(y);
Week 6 GRPA 1
public static void displayPlayers(ArrayList<CricketPlayer> bw,
ArrayList<CricketPlayer> bt) {
for (CricketPlayer player : bw) {
System.out.print(player.getName() + " ");
}
System.out.println();
for (CricketPlayer player : bt) {
System.out.print(player.getName() + " ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CricketPlayer p1 = new CricketPlayer(sc.next(), sc.nextInt(), sc.nextInt(),
sc.nextInt());
CricketPlayer p2 = new CricketPlayer(sc.next(), sc.nextInt(), sc.nextInt(),
sc.nextInt());
CricketPlayer p3 = new CricketPlayer(sc.next(), sc.nextInt(), sc.nextInt(),
sc.nextInt());
CricketPlayer p4 = new CricketPlayer(sc.next(), sc.nextInt(), sc.nextInt(),
sc.nextInt());
ArrayList<CricketPlayer> bw = new ArrayList<>();
ArrayList<CricketPlayer> bt = new ArrayList<>();
// Checking if players are bowlers or batsmen and adding them to the
respective lists
if (p1.avgWickets() > 1) {
bw.add(p1);
}
if (p1.avgRuns() > 25) {
bt.add(p1);
}
if (p2.avgWickets() > 1) {
bw.add(p2);
}
if (p2.avgRuns() > 25) {
bt.add(p2);
}
if (p3.avgWickets() > 1) {
bw.add(p3);
}
if (p3.avgRuns() > 25) {
bt.add(p3);
}
if (p4.avgWickets() > 1) {
bw.add(p4);
}
if (p4.avgRuns() > 25) {
bt.add(p4);
}
Week 6 GRPA 2
Stack<Character> stack = new Stack<>();
for (char c : sequence.toCharArray()) {
if (c == '{' || c == '(' || c == '[') {
stack.push(c);
} else if (c == '}' || c == ')' || c == ']') {
if (stack.isEmpty()) {
return false;
} else if (c == '}' && stack.peek() == '{') {
stack.pop();
} else if (c == ')' && stack.peek() == '(') {
stack.pop();
} else if (c == ']' && stack.peek() == '[') {
stack.pop();
} else {
return false;
}
}
}
return stack.isEmpty();