You are on page 1of 29

Problem Statement - Find Maximum and Minimum

Age
Complete the main method to accept the age of n students and find the
maximum and minimum age .

The first input is the number n representing the number of age values you
need to enter as integers

Followed by the age values separated by space.

The output should display as shown below in sample input /output.

Following requirements should be taken care in the program.

1. Input should be taken through Console


2. Program should print the output as described in the Example Section
below
3. The number n representing the number of students should be allowed in
the range of 1 to 20
4. If n is entered less than 1 or more than 20 , it should print message as
INVALID_INPUT.

Example
Sample Input 1:
5
34 56 12 89 43

Sample Ouptut 1:
MIN=12
MAX=89
Sample Input 2:
25
Expected Output:
INVALID_INPUT
Sample Input 3:
8
78 44 23 65 45 9 23 39

Expected Output:
MIN=9
MAX=78
import java.io.*;

import java.util.*;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Scanner; /** * */

public class Source {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int a;

a = sc.nextInt();

if(a<=0){

System.out.println("INVALID_INPUT");

else if(a>20){

System.out.println("INVALID_INPUT");

else{

int[] nums= new int[a];

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

//for reading array

nums[i]=sc.nextInt();

Arrays.sort(nums);

System.out.println("MIN=" + nums[0]);

System.out.println("MAX=" + nums[nums.length-1]);

}
Coding Java : Conditional Statements 1
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

Complete the main method of Source class to accept marks of a student in


physics,chemistry and mathematics. Calculate average marks and display the
grade based on the following criteria.

averageMarks>=70 DISTINCTION
averageMarks>=60 FIRST
averageMarks>=50 SECOND
averageMarks>=40 THIRD
averageMarks<40 FAIL

Following requirements should be taken care in the program.

1. Input should be taken through Console separated by space or new line


2. Input marks as integer values.
3. Program should print the output as described in the Example Section
below
4. If any of the marks value is lesser than zero or greater than 100 then
the output should show as INVALIDMARKS

Example
Sample Input 1:
45
67
89

Expected Output:
FIRST
Sample Input 2:
24
45
34

Expected Output:
FAIL
Sample Input 3:
80
90
87

Expected Output :
DISTINCTION
Sample Input 4:
-44
34
78

Expected Output :
INVALIDMARKS
Sample Input : 5
45
46
51

Expected Output :
THIRD
Sample Input: 60
104
90
80

Expected Output:
INVALIDMARKS

import java.util.Scanner;

public class Source {

public static void main (String[] args){

Scanner sc = new Scanner(System.in);

double d,averageMarks;

Double a = sc.nextDouble();

Double b = sc.nextDouble();

Double c = sc.nextDouble();

d = a+b+c;

averageMarks = d/3;

if(a>100){

System.out.println("INVALIDMARKS");

}
else if (b>100){

System.out.println("INVALIDMARKS");

else if (c>100){

System.out.println("INVALIDMARKS");

else if (a<0){

System.out.println("INVALIDMARKS");

else if (b<0){

System.out.println("INVALIDMARKS");

else if (c<0){

System.out.println("INVALIDMARKS");

else{

if(averageMarks>=70){

System.out.println("DISTINCTION");

else if (averageMarks>=60){

System.out.println("FIRST");

else if (averageMarks>=50){

System.out.println("SECOND");

else if (averageMarks>=40){

System.out.println("THIRD");

else{
System.out.println("FAIL");

}
Coding Java : Loop 2
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

Complete the main method to accept two integers and display the sum of all
the prime numbers between these two numbers.

Following requirements should be taken care in the program.

1. Input should be taken through Console


2. Program should print the output as described in the Example Section
below
3. The two input numbers are considered inclusive while finding sum of
prime numbers between these two numbers.
4. The minimum number allowed as input is 3 and maximum number
allowed is 1000. If any number is accepted as input which is below 3 or larger
than 1000 it should show error message as INVALID_INPUT
5. The first input value should be smaller than the second input
value.Otherwise it should show error message as INVALID_INPUT

Example
Sample Input 1:
10 20
Expected Output:
60
Sample Input 2:
2 30
Expected Output:
INVALID_INPUT
Sample Input 3:
2 40
Expected Output:
INVALID_INPUT
Sample Input 4:
10 1020
Expected Output:
INVALID_INPUT
Sample Input 5:
10 1000

Expected Output:
76110
Sample Input 6:
20 10
Expected Output:
INVALID_INPUT

import java.util.Scanner;

import java.util.*;

/** * Main class */

public class Source {

/** * Main method * @param args */

public static void main(String[] args) {

// Student Code begins

Scanner sc =new Scanner(System.in);

int a= sc.nextInt();

int b=sc.nextInt();

int sum=0;

int count=0;

int i,j;

if(a>b||a<3||a>1000||b<3||b>1000||b<a) {

System.out.println("INVALID_INPUT");

else {

for(i=a;i<b+1;i++) {

count=0;

for(j=2;j<i;j++) {

if(i%j==0) {

count++;

break;

}
}

if(count==0) {

sum=sum+i;

System.out.println(sum);

} // Student Code ends

Coding Java : Arrays 2


bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

Complete the main method to Accept n numbers and display the numbers in
ascending order as output ,if n is even. If n is odd, then display the numbers in
descending order

Following requirements should be taken care in the program.

1. Input should be taken through Console


2. Program should print the output as described in the Example Section
below
3. The first input n should represent the total number of values entered
followed by the actual values to be sorted.
4. n should be within the range of 1 to 20 . If n is entered as less than 1 or
more than 20 , it should show message as INVALID_INPUT.

Example
Sample Input 1:
7
23 45 67 97 65 34 74

Expected Output:
97 74 67 65 45 34 23
Sample Input 2:
6
77 44 22 65 28 43

Expected Output2:
22 28 43 44 65 77
Sample Input 3:
0

Expected Output 3:
INVALID_INPUT
Sample Input 4:
30

Expected Output 4:
INVALID_INPUT

import java.util.Scanner;

import java.util.Arrays;

import java.util.Collections;

/** */ public class Source {

/** * Main method * @param args */

public static void main(String[] args) {

int n; Scanner in = new Scanner(System.in);

n = in.nextInt();

if(n<1 || n>20) {

System.out.println("INVALID_INPUT");

return;

else {

int[] arr = new int[n];

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

arr[i] = in.nextInt();

if(n%2 == 0) {

Arrays.sort(arr);

for(int i=0;i<n;i++) {
System.out.print(arr[i]);

System.out.print(" ");

else { Arrays.sort(arr);

for(int i=n-1;i>=0;i--) {

System.out.print(arr[i]);

System.out.print(" ");

Coding Java : Conditional Statements 2

bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

Complete the main method of the class Source with appropriate code to
accept all the 3 sides of a triangle and display if the triangle is a right angle
triangle or not.

A rigt angle triangle is a triangle whose sum of squares of two sides will result
in the square of the third side.

Following requirements should be taken care in the program.

1. Input should be taken through Console


2. Program should print the output as described in the Example Section
below
3. The input values representing sides of the triangle must be accepted in
the decreasing order of their length.
4. Take sides as integers.

Example
Sample Input 1:
5 4 3
Expected Output:
RIGHT ANGLE
Sample Input 2:
7 6 5
Expected Output:
NOT RIGHT ANGLE
Sample Input 3:
13 12 5
Expected Output:
RIGHT ANGLE
Sample Input 4:
4 6 8
Expected Output:
INVALID_INPUT

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

// Class name should be "Source",

// otherwise solution won't be accepted

public class Source {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

// Declare the variable

int right1, right2;

int side1,side2, side3;

side1= in.nextInt();

side2= in.nextInt();
side3= in.nextInt();

right1=side1*side1;

if(side1>side2 && side2>side3) {

right2=(side2*side2)+(side3*side3);

if(right2==right1) {

System.out.println("RIGHT ANGLE");

else {

System.out.println("NOT RIGHT ANGLE");

else {

System.out.println("INVALID_INPUT");

Coding Java : Loop 3


bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

Complete the main method to find whether a given 3-digit number is an


Armstrong number or not.
An Armstrong number of three digits is an integer such that the sum of the
cubes of its digits is equal to the number itself. For example, 371 is an
Armstrong number since 33 + 73 + 13 = 371.

Following requirements should be taken care in the program.

1. Input should be taken through Console


2. Program should print the output as described in the Example Section
below
3. If the number is not 3 digit output should show as INVALID_INPUT

Example
Sample Input 1:
371
Expected Output:
ARMSTRONG
Sample Input 2:
832
Expected Output:
NOT ARMSTRONG
Sample Input 3:
153
Expected Output:
ARMSTRONG
Sample Input 4:
963
Expected Output:
NOT ARMSTRONG
Sample Input 5:
45
Expected Output:
INVALID_INPUT

import java.util.Scanner;

public class Source{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int res=0,r,count=0,number;

number=n;

while(n>0)

{
n=n/10;

count++;

if(count==3)

n=number;

while(number>0)

r=number%10;

res+=Math.pow(r,3);

number=number/10;

if(res==n)

System.out.println("ARMSTRONG");

else

System.out.println("NOT ARMSTRONG");

else

System.out.println("INVALID_INPUT");

}
Customer Information
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION
Problem Statement - Customer Information
Create a class named SimpleDate with the below private instance
variables

day:int
month:int
year:int

 create constructor with parameter sequence (day,month,year)


 create Getters
 create setDate method with 3 parameters for changing date values
 setDate(int, int, int) : void //parameter sequence (day,month,year)
 Override toString() method to return String as per below format

x/y/z

where x is day, y is month and z is year ex. 1/1/2018

 create a static method as below to validate date. It should return false, if


date is not valid as per given rules
 validateDate(SimpleDate):boolean

Rules for date validation

 Year cannot be less than 2000


 month should be from 1 to 12
 day should be 30 or 31 based on the month
 for February, days are always 28 (No leap year validation required)

Create a class named Address with the below private instance variable

area:String
city:String

 create constructor with parameter sequence (area,city)


 create getters and setters
 Override toString() method to return String as per below format

xx, yy

where xx is area and yy is city

Create a class named Customer with the below private instance


variables

custId:int
name:int
address:Address
registrationDate:SimpleDate

 create constructor with parameter


sequence(custId,name,address,registrationDate). If registrationDate is invalid,
set it to null
 create getter and setter for address and registrationDate. If
registrationDate is invalid, set it to null
 Override toString to return String as per below format
Id : xxx, Name : xxxx
Address : xxxx, xxxx
Registered on : xxxx

If the address or registration date is null, its value should be given as


"Unknown"

Id : xxx, Name : xxxx


Address : Unknown
Registered on : Unknown

In the class Source, do the following

In the main method, - Accept the inputs using Console as shown in the
Example section - Create object of Customer with aggregated objects of
address and registration date - print the Customer details as shown in
Example section

Example
Sample Input:
101 john //id name
HSR Bangalore //area city
1 1 2019 //day month year

Expected Output:
Id : 101, Name : john
Address : HSR, Bangalore
Registered on : 1/1/2019
Sample Input:
101 Dave
BTM Bangalore
30 1 1900

Expected Output:
Id : 101, Name : Dave
Address : BTM, Bangalore
Registered on : Unknown

import java.util.Scanner;
class SimpleDate {

private int day;

private int month;

private int year;

public SimpleDate(){}

public SimpleDate(int day, int month, int year) {

this.day=day; this.month=month; this.year= year;

} public int getDay() {

return day;

public int getMonth() { return month; }

public int getYear() { return year; }

public void setDate(int day,int month,int year) {

this.day=day; this.month=month; this.year=year;

@Override public String toString() {

return day+"/"+month+"/"+year;

public boolean validateDate(SimpleDate sd) {

if(sd.year<2000) { return false; }

if(sd.month<1 || sd.month>12) { return false; }

if(sd.month==2 && (sd.day>28 || sd.day <1)) { return false; }

if((sd.month==4 ||sd.month==6 ||sd.month== 9 ||sd.month== 11) && (sd.day>30 ||


sd.day<1)) { return false; }

if((sd.month==1 || sd.month==3 || sd.month==5 || sd.month==7 || sd.month==8 ||


sd.month==10 ||sd.month==12) && (day>31 && day<1)) { return false; }

return true;

}
}

Colour Code Validator


bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION
Problem Statement - Colour Code Validator
Main class Source should have the functionality to validate the input
hexadecimal and decimal colour codes.

Create two static methods in class ColourCodeValidator as per the


below signature

validateHexCode(String):int
validateDecimalCode(String):int

Both the methods return 1 for valid codes and -1 for invalid
codes. Rules for valid codes are given below

Hexadecimal code rules

 Format: #A1BC23
 Must start with "#" symbol
 Must contain six characters after #
 It may contain alphabets from A-F or digits from 0-9
Decimal code rules

 Format: rgb(x,y,z)
 x,y and z are values from 0 to 255 inclusive

In the main method , do the following

 Accept the inputs using Console as shown in the Example section


 First input is choice based on which one of the static methods should be
invoked
 choice 1 is for validating the input hexadecimal colour code
 choice 2 is for validating the input decimal colour code
 Display Valid code or Invalid code based on the validation result
 If the choice is neither 1 or 2, display message "Invalid choice"

Example
Sample Input:
1 #ABCDEF

Expected Output:
Valid Code
Sample Input:
2 rgb(9,99,249)

Expected Output:
Valid Code
Sample Input:
9

Expected Output:
Invalid choice

import java.util.*;

import java.text.*;

import java.util.regex.Pattern;

import java.util.regex.Matcher;

public class Source {


public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int x = in.nextInt();

if (x==1){

String z=in.next();

int y=validateHexCode(z);

if (y==1) {

System.out.print("Valid Code"); }

else{ System.out.print("Invalid code"); }

else if (x==2) {

String z=in.next();

int y=validateDecimalCode(z);

if (y==1) { System.out.print("Valid Code"); }

else{ System.out.print("Invalid code"); } }

else{ System.out.print("Invalid choice"); } }

public static int validateHexCode(String s){

List<Character> l = new ArrayList<>();

l.add('A'); l.add('B'); l.add('C'); l.add('D'); l.add('E'); l.add('F'); l.add('0'); l.add('1'); l.add('2');


l.add('3'); l.add('4'); l.add('5'); l.add('6'); l.add('7'); l.add('8'); l.add('9');

if(s.length()!=7)return -1;

if(s.charAt(0)!='#')return -1;

for(int i=1;i<7;i++){

if(l.contains(s.charAt(i))){ }

else{ return -1; } } return 1; }

public static int validateDecimalCode(String s){

if(s.startsWith("rgb") == false)return -1;

s = s.substring(4,s.length()-1);

String str[] = s.split(",");

if(str.length!=3)return -1;
try{ int x = Integer.parseInt(str[0]);

if(x<0||x>255)return -1;

x = Integer.parseInt(str[1]);

if(x<0||x>255)return -1;

x = Integer.parseInt(str[2]);

if(x<0||x>255)return -1; }

catch(Exception e){ return -1; } return 1;

}}

Account Details
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION
Problem Statement - Account Details
Complete the class Account and AccountDetails as per the below requirement

class Account :

Create the following instance/static members:

accountNo : int
balance : double
accountType : String
counter :int static

Define parameterized constructor with two parameters to initialize balance and


accountType. accountNo should be initialized by incrementing counter.

 Implement the below operations:


 void depositAmount(double amount)

 To add amount to account balance

 void printAccountDetails()

 To display account details as per format given in Example Section


class AccountDetails :

 Create main method and follow the below instructions.

 Accept balance, account type and amount as input for two account
objects from Console(Refer Example section for input format)

 create first object using the input data and display account details

 Deposit amount using the input data and display the new account
balance

 create second account object using the input data and display account
details.

 Set account balance to new balance using input data and display the
new account balance

Example
Sample Input:
100.5 Savings 25.5 // balance type amount
for first account
200 Current 50.5 // balance type amount
for second account

Expected Output:
[Acct No : 1, Type : Savings, Balance : 100.5]
New Balance : 126.0
[Acct No : 2, Type : Current, Balance : 200.0]
New Balance : 50.5
Sample Input:
0 Current 100
0 Current 50

Expected Output:
[Acct No : 1, Type : Current, Balance : 0.0]
New Balance : 100.0
[Acct No : 2, Type : Current, Balance : 0.0]
New Balance : 50.0

import java.util.Scanner;

class Account{

static int counter=0;

int accountNo;
double balance;

String accountType;

Account(double balance, String accountType){

accountNo = ++counter;

this.balance = balance;

this.accountType = accountType;

public void depositAmount(double amount){

this.balance = this.balance + amount;

public void printAccountDetails(){

System.out.println("[Acct No : "+accountNo+", Type : "+accountType+", Balance:


"+balance+"]");

public double getBalance(){ return balance; }

class Source{

public static void main(String args[]){

Scanner s = new Scanner(System.in);

double balance,amount,balance2,amount2;

String acctType,acctType2;

balance = s.nextDouble();

acctType = s.next();

amount = s.nextDouble();

//System.out.println(balance+acctType+amount);

s.nextLine();

balance2 = s.nextDouble();

acctType2 = s.next();

amount2 = s.nextDouble();
Account a1 = new Account(balance,acctType);

a1.printAccountDetails();

a1.depositAmount(amount);

System.out.println("New Balance : "+a1.getBalance());

Account a2 = new Account(balance2,acctType2);

a2.printAccountDetails();

a2.depositAmount(amount2);

System.out.println("New Balance : "+a2.getBalance());

Player Rating
bookmark_border
 subject Coding
 casino 50 points

DESCRIPTION
Problem Statement
Create a class "Player" with the following members:

 firstName: String
 lastName: String
 Player(firstName: String, lastName: String)
 getName(): String
 abstract getRating(): int

The method getName() should return the fullname of the player which is a
combination of firstName and lastName separated by a single space.

Create a subclass "CricketPlayer" with the following members:


 averageRuns: double
 CricketPlayer(firstName: String, lastName: String, averageRuns:
double)
 getRating(): int

The rating of a cricket player is based on the following slab:

 if averageRuns > 55 then 7


 if averageRuns > 50 then 6
 if averageRuns > 40 then 5
 if averageRuns > 30 then 3
 if averageRuns > 20 then 2
 if averageRuns <=20 then 1

Create a subclss "FootballPlayer" with the following members:

 goals: int
 FootballPlayer(firstName: String, lastName: String, goals: int)
 getRating(): int

The rating of a football player is based on the following slab:

 if goals > 20 then 5


 if goals > 15 then 4
 if goals > 10 then 3
 if goals > 5 then 2
 if goals <=5 then 1

Instructions
 Ensure your code compiles without any errors/warning/deprecations
 Follow best practices while coding
 Avoid too many & unnecessary usage of white spaces (newline,
spaces, tabs, ...), except to make the code readable
 Use appropriate comments at appropriate places in your exercise, to
explain the logic, rational, solutions, so that evaluator can know them
 Try to retain the original code given in the exercise, to avoid any issues
in compiling & running your programs
 Always test the program thoroughly, before saving/submitting
exercises/project

abstract class Player{


private String firstName; private String lastName;

public Player(String firstName,String lastName) {

this.firstName=firstName;

this.lastName=lastName;

public String getName() {

return this.firstName+" "+this.lastName;

public abstract int getRating();

class CricketPlayer extends Player{

private double averageRuns;

public CricketPlayer(String firstName,String lastName,double averageRuns) {

super(firstName,lastName);

this.averageRuns=averageRuns;

public int getRating() {

int i=1,j=0;

if(this.averageRuns>55 && i==1) {

i=0; j=7;

if(this.averageRuns>50 && i==1) {

i=0; j=6;

if(this.averageRuns>40 && i==1) {

i=0; j=5;

if(this.averageRuns>30 && i==1) { i=0; j=3; }


if(this.averageRuns>20 && i==1) { i=0; j=2; }

if(this.averageRuns<=20 && i==1) { i=0; j=1; }

return j;

class FootballPlayer extends Player{

private int goal;

public FootballPlayer(String firstName,String lastName,int goal) {

super(firstName,lastName); this.goal=goal;

} public int getRating() {

int i=1,j=0;

if(this.goal>20 && i==1) { i=0; j=5; }

if(this.goal>15 && i==1) { i=0; j=4; }

if(this.goal>10 && i==1) { i=0; j=3; }

if(this.goal>5 && i==1) { i=0; j=2; }

if(this.goal<=5 && i==1) { i=0; j=1; }

return j; }

public class Source{ public static void main(String[] args){ } }

You might also like