0% found this document useful (0 votes)
2 views5 pages

Java Practice Sheet Answers

The document contains a series of Java programming exercises aimed at learners, including calculating the weight of a school bag, converting days to hours, sharing cookies fairly, and calculating bus fare. Each exercise includes a code snippet, an explanation of the logic behind the code, and the use of appropriate data types. The document also covers additional exercises related to discounts, average calculations, unit conversions, and finding the cube of a number.

Uploaded by

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

Java Practice Sheet Answers

The document contains a series of Java programming exercises aimed at learners, including calculating the weight of a school bag, converting days to hours, sharing cookies fairly, and calculating bus fare. Each exercise includes a code snippet, an explanation of the logic behind the code, and the use of appropriate data types. The document also covers additional exercises related to discounts, average calculations, unit conversions, and finding the cube of a number.

Uploaded by

agarwalmona410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Worksheet 1

Question 1: Calculate School Bag Weight


A student has a textbook weighing 1.2 kg, a notebook weighing 0.5 kg, and a lunchbox weighing
0.8 kg. Write a program to calculate and print the total weight of the school bag.
Answer:

public class BagWeight {


public static void main() {
double textbook = 1.2;
double notebook = 0.5;
double lunchbox = 0.8;
double totalWeight = textbook + notebook + lunchbox;
[Link]("Total Bag Weight (kg): " + totalWeight);
}
}

Explanation for Learners:


Why double? We use the double data type because the weights have decimal points (like 1.2
and 0.5). If we used int, Java would throw away the decimals!
What is happening? We create three storage boxes (variables) to hold our numbers. Then, we
create a fourth box called totalWeight to hold the final sum when we add them all together (+).
Finally, [Link] prints our result on the screen.
Question 2: Convert Days to Hours
Write a program that takes a number of days (e.g., 5 days) and converts it entirely into hours,
displaying the final result.
Answer:

public class DaysToHours {


public static void main() {
int days = 5;
int hours = days * 24;
[Link]("Total Hours: " + hours);
}
}

Explanation for Learners:


Why int? We use int (integer) here because whole numbers like 5 days and 24 hours do not
have any decimal parts.
The Math: Since 1 single day always contains 24 hours, converting days to hours means we just
need to multiply the number of days by 24. In Java, multiplication is done using the asterisk
symbol (*).
Question 3: Sharing Cookies Fairly
A box contains 32 cookies to be divided equally among 6 friends. Write a program using the
modulus operator (%) to find out how many cookies will be left over after sharing them equally.
Answer:

public class ShareCookies {


public static void main() {
int cookies = 32;
int friends = 6;
int leftover = cookies % friends;
[Link]("Leftover cookies: " + leftover);
}
}

Explanation for Learners:


What does % do? The % symbol is called the modulus operator. It does not give you the answer
to a division question; instead, it only looks for the remainder (what is left over).
The Math: If 6 friends take 5 cookies each, they eat 30 cookies in total ($6 \times 5 = 30$).
Since we started with 32 cookies, there are exactly 2 cookies sitting left over in the box. The %
tool automatically finds that remaining 2 for us.
Question 4: Calculate Total Bus Fare
A group of 4 friends are traveling by bus. If a single ticket costs 45 dollars, write a program to
calculate and print the total fare for the entire group.
Answer:

public class BusFare {


public static void main() {
int ticketCost = 45;
int friendsCount = 4;
int totalFare = ticketCost * friendsCount;
[Link]("Total Bus Fare: " + totalFare);
}
}

Explanation for Learners:


What is happening? Think of this like buying items at a store checkout counter. If one ticket
costs 45 dollars, and you need to buy 4 of them, you need to multiply the price of one single
ticket by the total number of people ($45 \times 4$).
We save both initial values in clean int variables and compute the final calculation using *.
Java Worksheet 2
Question 1: Calculate Toy Store Discount
A remote-controlled car costs 1200 dollars. The store offers a flat discount of 150 dollars. Write
a program to calculate the final price the customer has to pay.
Answer:

public class ToyDiscount {


public static void main() {
int originalPrice = 1200;
int discount = 150;
int finalPrice = originalPrice - discount;
[Link]("Final Price: " + finalPrice);
}
}

Explanation for Learners:


The Logic: A discount means you get to pay less money than the original price tag. To find out
how much money you actually need to hand over to the cashier, you take away (subtract) the
discount value from the original price tag.
In Java programming, we perform basic subtraction using a regular minus dash (-).
Question 2: Find Monthly Pocket Money Average
A teenager receives 500 dollars in January, 650 dollars in February, and 400 dollars in March.
Write a program to calculate their average pocket money per month over this period (Hint: Use a
data type that preserves decimal values for the average).
Answer:

public class PocketMoneyAverage {


public static void main() {
int jan = 500;
int feb = 650;
int mar = 400;
double average = (jan + feb + mar) / 3.0;
[Link]("Average Pocket Money: " + average);
}
}
Explanation for Learners:
How to find an average: To find an average value, you must first add all individual items
together, and then divide that big total by the number of items you counted (3 months in this
scenario).
Crucial Detail: Notice the parentheses (jan + feb + mar). This forces Java to do the addition first
before doing division. Also, dividing by 3.0 instead of a plain 3 forces Java to give us a highly
accurate decimal response stored safely inside a double variable.
Question 3: Convert Meters to Centimeters
Write a program to convert a distance given in meters (e.g., 8 meters) into centimeters (1 meter
= 100 cm) and print the result.
Answer:

public class MetersToCm {


public static void main() {
int meters = 8;
int centimeters = meters * 100;
[Link]("Distance in Centimeters: " + centimeters);
}
}

Explanation for Learners:


The Conversion: Because every single meter contains 100 separate centimeters, centimeter
measurements will always look like a much larger number than meter measurements. To
change meters into centimeters, we must multiply the number of meters by 100.
Question 4: Find Cube of a Number
Write a program to store an integer in a variable (e.g., int num = 4;) and calculate its cube
($num \times num \times num$) using basic arithmetic operators.
Answer:

public class CubeOfNumber {


public static void main() {
int num = 4;
int cube = num * num * num;
[Link]("Cube of the number: " + cube);
}
}
Explanation for Learners:
What is a Cube? In math, "cubing" a number does not mean multiplying it by 3. It means
multiplying the number by itself, and then multiplying by itself once more ($4 \times 4 \times 4$).
We write this straight out in Java code by chaining multiplication steps: num * num * num.

You might also like