Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

/******************************************************************************

File name: Challenge1.java


Author: Kathiravan S
Date: 23 Dec 2019
*******************************************************************************/

import java.util.Scanner;

abstract class Themepark {


int adultCost = 500;
int childCost = 300;

int calculateCost(int m, int n) {


return adultCost * n + childCost * m;
}
abstract void playGame();
}

class Queensland extends Themepark {

public Queensland(int[] playedGames) {


this.playedGames = playedGames;
}

int[] playedGames;
boolean[] games = new boolean[30];
int i = 0;

@Override
void playGame() {
System.out.println("Welcome to Queensland!");
for (i = 0; i < playedGames.length; i++) {
if (!games[playedGames[i]]) {
games[playedGames[i]] = true;
System.out.println("Playing game " + playedGames[i] + " at Queensland");
} else {
System.out.println("You have already played game " + playedGames[i]);
}

}
}
class Wonderla extends Themepark {

public Wonderla(int[] playedGames) {


this.playedGames = playedGames;
}

int[] playedGames;
boolean[] games = new boolean[40];
int i = 0;

@Override
void playGame() {
System.out.println("Welcome to Wonderla!");
for (i = 0; i < playedGames.length; i++) {
if (!games[playedGames[i]]) {
games[playedGames[i]] = true;
System.out.println("Playing game " + playedGames[i] + " at Wonderla");
} else {
System.out.println("Playing game " + playedGames[i] + " again at Wonderla");
}

}
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in).useDelimiter("\n");

int choice = sc.nextInt();

String str = sc.next();


String[] str2 = str.split(" ");

int size = str2.length;


int[] arr = new int[size];

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


arr[i] = Integer.parseInt(str2[i]);
}

if (choice == 1) {
Queensland queensland = new Queensland(arr);
queensland.playGame();
} else if (choice == 2) {
Wonderla wonderla = new Wonderla(arr);
wonderla.playGame();
}
}
}
/******************************************************************************
File name: Challenge2.java
Author: Kathiravan S
Date: 23 Dec 2019
*******************************************************************************/

import java.util.Scanner;

interface Growing {
void isGrowing();
}

class GrowingNumber implements Growing {

int number;
boolean flag = true;

public GrowingNumber(int number) {


this.number = number;
}

@Override
public void isGrowing() {
while (number > 1) {
int ones = number % 10;
int tens = ((number - ones) / 10) % 10;

number = number / 10;


if (tens > ones) {
flag = false;
}
}
if (flag) {
System.out.println("Growing number");
} else
System.out.println("Not growing number");
}
}

class GrowingString implements Growing {

String string;
boolean flag = true;

public GrowingString(String string) {


this.string = string;
}

@Override
public void isGrowing() {
char[] arr = string.toCharArray();
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
flag = false;
}
}
if (flag) {
System.out.println("Growing string");
} else
System.out.println("Not growing string");
}
}

class Main2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
String string = scanner.nextLine();

GrowingNumber growingNumber = new GrowingNumber(num);


growingNumber.isGrowing();

GrowingString growingString = new GrowingString(string);


growingString.isGrowing();
}
}
/******************************************************************************
File name: Challenge3.java
Author: Kathiravan S
Date: 23 Dec 2019
*******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

abstract class Account {


String name;
int number;
int balance;
Date startDate;

abstract double calculateInterest(Date dueDate);

public int monthsDifference(Date startDate, Date dueDate) {


Calendar c1 = new GregorianCalendar();
c1.setTime(startDate);

Calendar c2 = new GregorianCalendar();


c2.setTime(dueDate);
int ans = (c2.get(c2.YEAR) - c1.get(c1.YEAR)) * 12;
ans += (c2.get(c2.MONTH) - c1.get(c1.MONTH));
return ans;
}
}

class CurrentAccount extends Account {

int rate = 5;
public CurrentAccount(String name, int number, int amount, Date startDate) {
this.name = name;
this.number = number;
this.balance = amount;
this.startDate = startDate;
}
@Override
double calculateInterest(Date dueDate) {
int months = monthsDifference(startDate, dueDate);
return (balance * rate * months / 1200);
}
}

class SavingsAccount extends Account {

int rate = 12;

public SavingsAccount(String name, int number, int amount, Date startDate) {


this.name = name;
this.number = number;
this.balance = amount;
this.startDate = startDate;
}
@Override
double calculateInterest(Date dueDate) {
int months = monthsDifference(startDate, dueDate);
return (balance * rate * months / 1200);
}
}

class Main3 {
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
int type = scanner.nextInt();

String name = scanner.next();


int number = scanner.nextInt();
int balance = scanner.nextInt();
String sStartDate = scanner.next();
String sEndDate = scanner.next();

Date startDate = new SimpleDateFormat("dd/MM/yyyy").parse(sStartDate);


Date endDate = new SimpleDateFormat("dd/MM/yyyy").parse(sEndDate);

if(type ==1){
SavingsAccount savingsAccount = new SavingsAccount(name, number, balance, startDate);
System.out.println(savingsAccount.calculateInterest(endDate));
}
else if(type ==2){
CurrentAccount currentAccount = new CurrentAccount(name, number, balance, startDate);
System.out.println(currentAccount.calculateInterest(endDate));
}

}
}
/******************************************************************************
File name: Challenge4.java
Author: Kathiravan S
Date: 23 Dec 2019
*******************************************************************************/

import java.util.Scanner;

abstract class Shape {


int length;
int width;
int radius;

abstract void calculatePerimeter();

abstract void calculateArea();


}

class Square extends Shape {


public Square(int width) {
this.width = width;
}

@Override
void calculatePerimeter() {
double perimeter = width * 4;
System.out.printf("Perimeter : %.2f\n", perimeter);
}

@Override
void calculateArea() {
double area = width * width;
System.out.printf("Area : %.2f\n", area);
}
}

class Rectangle extends Shape {


public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}

@Override
void calculatePerimeter() {
double perimeter = width * 2 + length * 2;
System.out.printf("Perimeter : %.2f\n", perimeter);
}

@Override
void calculateArea() {
double area = width * length;
System.out.printf("Area : %.2f\n", area);
}
}

class Circle extends Shape {


public Circle(int radius) {
this.radius = radius;
}

@Override
void calculatePerimeter() {
double perimeter = 2 * 3.141592 * radius;
System.out.printf("Circumference : %.2f\n", perimeter);
}

@Override
void calculateArea() {
double area = 3.141592 * radius * radius;
System.out.printf("Area : %.2f\n", area);
}
}

class Triangle extends Shape {


public Triangle(int radius, int length, int width) {
this.radius = radius;
this.width = width;
this.length = length;
}

@Override
void calculatePerimeter() {
double perimeter = length + width + radius;
System.out.printf("Perimeter : %.2f\n", perimeter);
}

@Override
void calculateArea() {
int p = (radius + width + length) / 2;
double area = Math.sqrt((p - radius) * (p - width) * (p - length) * p);
System.out.printf("Area : %.2f\n", area);
}
}

class Main4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char choice = scanner.next().charAt(0);

switch (choice) {
case 'S':
int len = scanner.nextInt();
Square square = new Square(len);
square.calculatePerimeter();
square.calculateArea();
break;

case 'R':
int width = scanner.nextInt();
int length = scanner.nextInt();
Rectangle rectangle = new Rectangle(width, length);
rectangle.calculatePerimeter();
rectangle.calculateArea();
break;

case 'C':
int rad = scanner.nextInt();
Circle circle = new Circle(rad);
circle.calculatePerimeter();
circle.calculateArea();
break;

case 'T':
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
Triangle triangle = new Triangle(a, b, c);
triangle.calculatePerimeter();
triangle.calculateArea();
break;
}
}
}

You might also like