0% found this document useful (0 votes)
7 views15 pages

Thithupe

Uploaded by

sketonboy2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Thithupe

Uploaded by

sketonboy2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

//QUESTION 1

import java.util.Scanner;
class Main {

public static void main(String args[]) {


String desert;
int step;
Scanner sc = new Scanner(System.in);
System.out.print("Enter desert: ");
desert = sc.nextLine();
System.out.print("Enter step: ");
step = Integer.parseInt(sc.nextLine());
System.out.println("1. Test getDesert()");
System.out.println("2. Test setStep()");
System.out.print("Enter TC (1 or 2): ");
int choice = Integer.parseInt(sc.nextLine());

Camel o = new Camel(desert, step);


switch (choice) {
case 1: {
System.out.println("OUTPUT: ");
System.out.println(o.getDesert());
break;
}
case 2: {
System.out.print("Enter new step: ");
step = Integer.parseInt(sc.nextLine());
o.setStep(step);
System.out.println("OUTPUT: ");
System.out.println(o.getStep());
break;
}
}
}
}
//QUESTION 1
public class Camel {
private String desert;
private int step;
public Camel() {}
public Camel(String desert, int step) {
this.desert = desert;
this.step = step;
}

public String getDesert() {


return desert.toUpperCase();
}

public int getStep() {


return step;
}
public void setStep(int step) {
this.step = step;
}
}
//QUESTION 3 : ICala.java
import java.util.List;

public interface ICala {


public int f1(List<Cala> t);
public void f2(List<Cala> t);
public void f3(List<Cala> t);
}
//QUESTION 3 : Cala.java
public class Cala {
private String owner;
private int price;

public Cala() {
}

public Cala(String owner, int price) {


this.owner = owner;
this.price = price;
}

public String getOwner() {


return owner;
}

public void setOwner(String owner) {


this.owner = owner;
}

public int getPrice() {


return price;
}

public void setPrice(int price) {


this.price = price;
}

}
//QUESTION 3 : MyCala.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MyCala implements ICala {

@Override
public int f1(List<Cala> t) {
int count = 0;
for (Cala o : t) {
if (Character.isDigit(o.getOwner().charAt(1))) {
count++;
}
}
return count;
}

@Override
public void f2(List<Cala> t) {
int count = 1;
int max = t.get(0).getPrice();

for (int i = 1; i < t.size(); i++) {


if (t.get(i).getPrice() > max) {
max = t.get(i).getPrice();
}
}

for (int i = 0; i < t.size(); i++) {


if (count == 0 && t.get(i).getPrice() == max) {
t.remove(i);
return;
}
if (t.get(i).getPrice() == max) {
count--;
}
}
}

@Override
public void f3(List<Cala> t) {
Collections.sort(t, new Comparator<Cala>() {
@Override
public int compare(Cala o1, Cala o2) {
return o1.getOwner().charAt(1) - o2.getOwner().charAt(1);
}
} );
}
}
//QUESTION 3 : Main.java
import java.util.ArrayList;
import java.util.Scanner;

class Main {

public static void main(String args[]) {


MyCala myCala = new MyCala();
ArrayList<Cala> list = new ArrayList<>();

Scanner sc = new Scanner(System.in);


System.out.print("Enter TC (1-f1;2-f2;3-f3): ");
int choice = Integer.parseInt(sc.nextLine());

switch (choice) {
case 1: {
Cala c1 = new Cala("A1B", 5);
Cala c2 = new Cala("BC2", 4);
Cala c3 = new Cala("CT", 3);
Cala c4 = new Cala("D3X", 4);
Cala c5 = new Cala("2E1", 5);
Cala c6 = new Cala("FY4", 2);
list.add(c1);
list.add(c2);
list.add(c3);
list.add(c4);
list.add(c5);
list.add(c6);
System.out.println("The list before running f1:");
for (Cala c : list) {
System.out.print("(" + c.getOwner() + "," + c.getPrice() + ")");
}
System.out.println("\nOUTPUT");
System.out.println(myCala.f1(list));
break;
}
case 2: {
Cala c1 = new Cala("A", 4);
Cala c2 = new Cala("C", 3);
Cala c3 = new Cala("B", 7);
Cala c4 = new Cala("D", 3);
Cala c5 = new Cala("E", 7);
Cala c6 = new Cala("F", 5);
list.add(c1);
list.add(c2);
list.add(c3);
list.add(c4);
list.add(c5);
list.add(c6);
System.out.println("The list before running f1:");
for (Cala c : list) {
System.out.print("(" + c.getOwner() + "," + c.getPrice() + ")");
}
myCala.f2(list);
System.out.println("\nOUTPUT");
for (Cala c : list) {
System.out.print("(" + c.getOwner() + "," + c.getPrice() + ")");
}
break;
}
case 3: {
Cala c1 = new Cala("A8", 1);
Cala c2 = new Cala("B1", 2);
Cala c3 = new Cala("C7", 3);
Cala c4 = new Cala("D2", 4);
Cala c5 = new Cala("E6", 5);
Cala c6 = new Cala("F3", 6);
list.add(c1);
list.add(c2);
list.add(c3);
list.add(c4);
list.add(c5);
list.add(c6);
System.out.println("The list before running f1:");
for (Cala c : list) {
System.out.print("(" + c.getOwner() + "," + c.getPrice() + ")");
}
myCala.f3(list);
System.out.println("\nOUTPUT");
for (Cala c : list) {
System.out.print("(" + c.getOwner() + "," + c.getPrice() + ")");
}
break;
}
}
}
}
//QUESTION 4 : IString.java
public interface IString {
public int f1(String str);
public String f2(String str);
}
//QUESTION 4 : MyString.java
import java.util.StringTokenizer;

public class MyString implements IString {

@Override
public int f1(String str) {
int count = 0;
StringTokenizer stk = new StringTokenizer(str);
while (stk.hasMoreTokens()) {
String s = stk.nextToken();
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
int n = Character.getNumericValue(s.charAt(i));
if (n % 2 == 1) {
count++;
break;
}
}
}
}

return count;
}

@Override
public String f2(String str) {
String result = "";
StringTokenizer stk = new StringTokenizer(str);
boolean isChanged = false;
while (stk.hasMoreTokens()) {
String s = stk.nextToken();
String reverse = "";
if (!isChanged) {
for (int i = s.length() - 1; i >= 0; i--) {
reverse += s.charAt(i);
}
}
if (!isChanged && reverse.equals(s)) {
isChanged = true;
result += " XX";
} else {
result += " " + s;
}
}
return result.substring(1);
}
}
//QUESTION 4 : Main.java
import java.util.Scanner;

class Main {

public static void main(String args[]) {


MyString o = new MyString();
Scanner sc = new Scanner(System.in);
System.out.println("1. Test f1()");
System.out.println("2. Test f2()");
System.out.print("Enter TC (1 or 2): ");
int choice = Integer.parseInt(sc.nextLine());

switch (choice) {
case 1: {
System.out.println("You don't need to enter a string: ");
String s = "a a1 b2 c34 d6";
System.out.println(s);
System.out.println("OUTPUT:");
System.out.println(o.f1(s));
break;
}
case 2: {
System.out.println("You don't need to enter a string: ");s
String s = "ab 12321 uv 12321 xy";
System.out.println(s);
System.out.println("OUTPUT:");
System.out.println(o.f2(s));
break;
}
}
}
}

You might also like