Java Code
Java Code
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
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;
int a;
a = sc.nextInt();
if(a<=0){
System.out.println("INVALID_INPUT");
else if(a>20){
System.out.println("INVALID_INPUT");
else{
for(int i=0;i<a;i++){
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
averageMarks>=70 DISTINCTION
averageMarks>=60 FIRST
averageMarks>=50 SECOND
averageMarks>=40 THIRD
averageMarks<40 FAIL
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;
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.
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.*;
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);
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
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;
n = in.nextInt();
if(n<1 || n>20) {
System.out.println("INVALID_INPUT");
return;
else {
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(" ");
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.
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.*;
side1= in.nextInt();
side2= in.nextInt();
side3= in.nextInt();
right1=side1*side1;
right2=(side2*side2)+(side3*side3);
if(right2==right1) {
System.out.println("RIGHT ANGLE");
else {
else {
System.out.println("INVALID_INPUT");
DESCRIPTION
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;
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
x/y/z
Create a class named Address with the below private instance variable
area:String
city:String
xx, yy
custId:int
name:int
address:Address
registrationDate:SimpleDate
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 {
public SimpleDate(){}
return day;
return day+"/"+month+"/"+year;
return true;
}
}
DESCRIPTION
Problem Statement - Colour Code Validator
Main class Source should have the functionality to validate the input
hexadecimal and decimal colour codes.
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
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
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;
int x = in.nextInt();
if (x==1){
String z=in.next();
int y=validateHexCode(z);
if (y==1) {
System.out.print("Valid Code"); }
else if (x==2) {
String z=in.next();
int y=validateDecimalCode(z);
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))){ }
s = s.substring(4,s.length()-1);
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; }
}}
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 :
accountNo : int
balance : double
accountType : String
counter :int static
void printAccountDetails()
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{
int accountNo;
double balance;
String accountType;
accountNo = ++counter;
this.balance = balance;
this.accountType = accountType;
class Source{
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);
a2.printAccountDetails();
a2.depositAmount(amount2);
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.
goals: int
FootballPlayer(firstName: String, lastName: String, goals: int)
getRating(): int
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
this.firstName=firstName;
this.lastName=lastName;
super(firstName,lastName);
this.averageRuns=averageRuns;
int i=1,j=0;
i=0; j=7;
i=0; j=6;
i=0; j=5;
return j;
super(firstName,lastName); this.goal=goal;
int i=1,j=0;
return j; }