0% found this document useful (0 votes)
2 views

2a.hands-On_Introduction to Java Muhindhar

The document outlines a hands-on training session focused on Java programming, featuring various exercises that cover variable declaration, data types, and basic programming concepts. Each exercise includes a problem statement, a suggested solution, and examples of expected input and output. The training aims to enhance participants' readiness for software development roles by practicing essential coding skills.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2a.hands-On_Introduction to Java Muhindhar

The document outlines a hands-on training session focused on Java programming, featuring various exercises that cover variable declaration, data types, and basic programming concepts. Each exercise includes a problem statement, a suggested solution, and examples of expected input and output. The training aims to enhance participants' readiness for software development roles by practicing essential coding skills.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

HANDS-ON

SDE Readiness Training

Hands-on No. : 2a

Topic : Introduction to Java

Date : 13.02.2025

Solve the following problems

Question
Question Detail Level
No.

Declare a variable ‘bookPrice’ (Choose the right datatype). Assign

1 the value 150.50 to ‘bookPrice’. Print the price. Now, re-assign a Easy
value to ‘bookPrice’ then print ‘bookPrice’.
SOLUTION
import java.util.Scanner;

public class BookPrice {


public static void main(String[] args) {
double bookPrice = 150.50;
System.out.println("Book Price: " + bookPrice);

bookPrice = 200.75;
System.out.println("Updated Book Price: " + bookPrice);
}
}
Create the variables for a player's name, age, height in cm,
weight in kg, rank, and mobile number, and assign the values of
2 Easy
your choice. Display the player detail. (byte, short, int, double,
String
datatypes can be used).
SOLUTION
public class PlayerDetails {
public static void main(String[] args) {
String playerName = "John Doe";
byte age = 25;
double heightCm = 180.5;
double weightKg = 75.3;
short rank = 5;
long mobileNumber = 9876543210;

System.out.println("Player Details:");
System.out.println("Name: " + playerName);
System.out.println("Age: " + age);
System.out.println("Height: " + heightCm + " cm");
System.out.println("Weight: " + weightKg + " kg");
It is going to be hard but, hard does not mean
impossible.
1
HANDS-ON
SDE Readiness Training
System.out.println("Rank: " + rank);
System.out.println("Mobile Number: " + mobileNumber);
}
}
Read a person's name first, read another person and another.
Greet the first person first, the third person second and the
3 Easy
second person last. If ‘Chloe’, ‘Joey’ & ‘Zoe’ are the inputs,
then the
output will be ‘Welcome Chloe! Welcome Zoe! Welcome Joey too!’
SOLUTION
import java.util.Scanner;

public class GreetPeople {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter first person's name: ");


String first_Person = scanner.nextLine();
System.out.print("Enter second person's name: ");
String second_Person = scanner.nextLine();
System.out.print("Enter third person's name: ");
String third-Person = scanner.nextLine();

System.out.println("Welcome " + firstPerson + "! Welcome "


+ thirdPerson + "! Welcome " + secondPerson + " too!");
}
}
A cashier in a shop has currency notes of denominations 10,50
and 100. If the amount to be returned is the input, find the total

4 number of currency notes of each denomination that the cashier Easy


should give to the customer. Write a program to accomplish the
above task. Assume that the input is in 10’s multiples.
SOLUTION:
import java.util.Scanner;

public class Cashier {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the amount to be returned");


int amount = scanner.nextInt();

int hundredNotes = amount / 100;


amount %= 100;
int fiftyNotes = amount / 50;
amount %= 50;
int tenNotes = amount / 10;

System.out.println("100s: " + hundredNotes);


System.out.println("50s: " + fiftyNotes);
System.out.println("10s: " + tenNotes);
}
}
It is going to be hard but, hard does not mean
impossible.
2
HANDS-ON
SDE Readiness Training
Write a Java program that increments a given number. Don't use
5 arithmetic operators. Easy
SOLUTION
import java.util.Scanner;

public class IncrementNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = scanner.nextInt();
scanner.close();

int incrementedNum = num + 1;


System.out.println("Incremented Number: " +
incrementedNum);
}
}
Write a program to find the maximum of two integers, without
using any conditional statements. The program should utilize
6 arithmetic operations or bitwise operations to determine the Easy
larger number.
SOLUTION:
import java.util.Scanner;

public class MaxOfTwo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");


int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
scanner.close();

int max = Math.max(a, b);

System.out.println("Maximum number: " + max);

It is going to be hard but, hard does not mean


impossible.
3
HANDS-ON
SDE Readiness Training
}
}
You have a green lottery ticket with three integers: a, b, and c.
The result of the ticket depends on the relationship between

7 these three numbers: Easy


1. If all three numbers are different, the result is 0.
2. If all three numbers are the same, the result is 20.

3. If exactly two numbers are the same, the result is 10.


Your task is to determine the result of the ticket based on the
values of a, b, and c.
Sample Input & Output
greenTicket(a :1, b : 2, c : 3) → 0
greenTicket(a: 2, b: 2, c: 2) → 20
greenTicket(a: 1, b:1, c: 2) → 10
SOLUTION
import java.util.Scanner;

public class GreenLotteryTicket {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int result;
System.out.print("Enter first number a: ");
int a = scanner.nextInt();
System.out.print("Enter second number b: ");
int b = scanner.nextInt();
System.out.print("Enter third number c: ");
int c = scanner.nextInt();

if (a == b && b == c) {
result = 20;
} else if (a == b || b == c || a == c) {
result = 10;
} else {
result = 0;
}

System.out.println("Ticket result: " + result);

It is going to be hard but, hard does not mean


impossible.
4
HANDS-ON
SDE Readiness Training

}
}
You need to create a package containing a total of goal kilos of
chocolate. You have two types of chocolate bars available:
 Small bars, each weighing 1 kilo.
 Big bars, each weighing 5 kilos.
Your task is to determine how many small bars you will need to
use, assuming you always use the big bars first to reach the
8 Easy
target weight. If it is not possible to exactly match the goal
weight using these bars, return -1.
Sample Input & Output
makeChocolate(small : 4, big : 1, goal : 9) → 4
makeChocolate(small : 4, big : 1, goal : 10) → -1
makeChocolate(small : 4, big : 1, goal : 7) → 2
SOLUTION:
import java.util.Scanner;

public class MakeChocolate {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of small bars: ");


int small = scanner.nextInt();
System.out.print("Enter number of big bars: ");
int big = scanner.nextInt();
System.out.print("Enter goal kilos: ");
int goal = scanner.nextInt();

int bigBarsUsed = goal / 5;


if (bigBarsUsed > big) {
bigBarsUsed = big;
}

int balWeight = goal - (bigBarsUsed * 5);

if (balWeight <= small) {

It is going to be hard but, hard does not mean


impossible.
5
HANDS-ON
SDE Readiness Training

System.out.println("Small bars needed: " + balWeight);


} else {
System.out.println("-1");
}
}
}
You are given three integer values: a, b, and c. Your task is to
return their sum, with the following condition:
 If any of the values is 13, it and all the values to its right
should not be included in the sum.
For example, if b is 13, then both b and c should not be included
9 Easy
in the sum.
Sample Input & Output
luckySum(a : 1, b : 2, c : 3) → 6
luckySum(a : 1, b : 2, c : 13) → 3
luckySum(a : 1, b : 13, c : 3) → 1
SOLUTION:
import java.util.Scanner;

public class LuckySum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();

int sum = 0;

if (a == 13) {
sum = 0;
} else if (b == 13) {
sum = a;
} else if (c == 13) {
sum = a + b;
} else {
sum = a + b + c;

It is going to be hard but, hard does not mean


impossible.
6
HANDS-ON
SDE Readiness Training

System.out.println(sum);
}
}
You are given three integers: a, b, and c. Your task is to return
the sum of their values after rounding each of them to the
nearest multiple of 10, based on the following rules:
1. If the rightmost digit of a number is 5 or more, round it
10 Easy
up to the next multiple of 10.
2. If the rightmost digit is less than 5, round the number
down to the previous multiple of 10.
For example:

 15 rounds up to 20.
 12 rounds down to 10.
Return the sum of the rounded values of a, b, and c.
Sample Input & Output
roundSum(a : 16, b : 17, c : 18) → 60
roundSum(a : 12, b : 13, c : 14) → 30
roundSum(a: 6, b : 4, c : 4) → 10
SOLUTION:
import java.util.Scanner;

public class RoundSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();

System.out.println(round(a) + round(b) + round(c));


}

public static int round(int num) {


int remainder = num % 10;

It is going to be hard but, hard does not mean


impossible.
7
HANDS-ON
SDE Readiness Training

if (remainder >= 5) {
return num + (10 - remainder); } else {
return num - remainder;
}
}
}

It is going to be hard but, hard does not mean


impossible.
8

You might also like