Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

03 Java Arrays Level 2 Lab Practice

The document outlines best programming practices, emphasizing the use of variables, proper naming conventions, and input validation. It includes sample Java programs demonstrating these practices, such as calculating the sum of digits in a number and working with multi-dimensional arrays. Additionally, it provides level 2 practice programs focused on employee bonuses, finding the youngest friend, and calculating BMI, among other tasks.

Uploaded by

dev gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

03 Java Arrays Level 2 Lab Practice

The document outlines best programming practices, emphasizing the use of variables, proper naming conventions, and input validation. It includes sample Java programs demonstrating these practices, such as calculating the sum of digits in a number and working with multi-dimensional arrays. Additionally, it provides level 2 practice programs focused on employee bonuses, finding the youngest friend, and calculating BMI, among other tasks.

Uploaded by

dev gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Best Programming Practice

1.​ All values as variables including Fixed, User Inputs, and Results
2.​ Avoid Hard Coding of variables wherever possible
3.​ Proper naming conventions for all variables
4.​ Proper Program Name and Class Name
5.​ Follow proper indentation
6.​ Give comments for every step or logical block like a variable declaration or conditional
and loop blocks
7.​ For every user input validate the user input, if invalid, state the error either exit the
program or ask user to enter again
8.​ Use Array length property while using for loop

1.​ Sample Program 1 - Create a program to find the sum of all the digits of a number given by
a user using an array and display the sum.
Hint =>
a.​ Take the input for a number and validate, if failed state and exit the program
b.​ Find the count of digits in the number
c.​ Find the digits in the number and save them in an array
d.​ Find the sum of the digits of the number and display the sum

Java
// Create SumOfDigit Class to compute the sum of all digits of a number using
// an array
import java.util.Scanner;

class SumOfDigits {
public static void main(String[] args) {
// Create a Scanner Object
Scanner input = new Scanner(System.in);

// Take input for a number


System.out.print("Enter a number: ");
int number = input.nextInt();

// Validate the user input number, if negative state invalid and exit
if (number < 0) {
System.err.println("Invalid Number.");
System.exit(0);
}

1
// Find the count of digits in the number
int count = 0;
int temp = number;
while (temp > 0) {
count++;
temp /= 10;
}

// Find the digits in the number and save them in an array


int[] digits = new int[count];
for (int i = 0; i < count; i++) {
digits[i] = number % 10;
number /= 10;
}

// Find the sum of the digits of the number


int sum = 0;
for (int i = 0; i < count; i++) {
sum += digits[i];
}

// Display the sum of the digits of the number


System.out.println("\nSum of Digits: " + sum);

// Close the Scanner Object


input.close();
}
}

2.​ Sample Program 2 - Working with Multi-Dimensional Arrays. Write a Java program to
create a 2 Dimensional (2D) array (matrix) of integers, initialize it with values, and print the
sum of all elements in the matrix
Hint =>
a.​ Take the input for a number of rows and columns
b.​ Create a 2D array (matrix) of integers
c.​ Take the input for the elements of the matrix
d.​ Calculate the sum of all elements in the matrix and display the sum
e.​ Also, Display the matrix

2
Java
// Program to create a 2D array, display the elements and calculate the sum of
// the elements of the array
import java.util.Scanner;

class 2DArray {
public static void main(String[] args) {
// Create a Scanner Object
Scanner input = new Scanner(System.in);

// Declare the 2D Array


int[][] arr = new int[3][3];

// Input the elements of the 2D Array


System.out.println("Enter the elements of the 2D Array: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = input.nextInt();
}
}

// Display the elements of the 2D Array and calculate the sum of the
// elements of the 2D Array
int sum = 0;
System.out.println("The elements of the 2D Array are: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j] + " ");
sum += arr[i][j];
}
System.out.println();
}

// Display the sum of the elements of the 2D Array


System.out.println("The sum of the elements of the 2D Array is: " + sum);

// Close the Scanner Object


input.close();
}
}

3
Level 2 Practice Programs
1.​ Create a program to find the bonus of 10 employees based on their years of service and the
total bonus amount the company Zara has to pay, along with the old and new salary.
Hint =>
a.​ Zara decides to give a bonus of 5% to employees whose year of service is more than 5
years or 2% if less than 5 years
b.​ Define a double array to save salary and years of service for each of the 10 employees
c.​ Also define a double array to save the new salary and the bonus amount as well as
variables to save the total bonus, total old salary, and new salary
d.​ Define a loop to take input from the user. If salary or year of service is an invalid number
then ask the use to enter again. Note in this case you will have to decrement the index
counter
e.​ Define another loop to calculate the bonus of 10 employees based on their years of
service. Save the bonus in the array, compute the new salary, and save in the array.
Also, the total bonus and total old and new salary can be calculated in the loop
f.​ Print the total bonus payout as well as the total old and new salary of all the employees
2.​ Create a program to find the youngest friends among 3 Amar, Akbar, and Anthony based on
their ages and the tallest among the friends based on their heights
Hint =>
a.​ Take user input for age and height for the 3 friends and store it in two arrays each to
store the values for age and height of the 3 friends
b.​ Loop through the array and find the youngest of the 3 friends and the tallest of the 3
friends
c.​ Finally display the youngest and tallest of the 3 friends
3.​ Create a program to store the digits of the number in an array and find the largest and
second largest element of the array.
Hint =>
a.​ Create a number variable and Take user input.
b.​ Define an array to store the digits. Set the size of the array to maxDigit variable initially
set to 10
c.​ Create an integer variable index with the value 0 to reflect the array index.
d.​ Use a loop to iterate until the number is not equal to 0.
e.​ Remove the last digit from the number in each iteration and add it to the array.
f.​ Increment the index by 1 in each iteration and if the index count equals maxDigit then
break out of the loop and the remaining digits are not added to the array
g.​ Define variable to store largest and second largest digit and initialize it to zero
h.​ Loop through the array and use conditional statements to find the largest and second
largest number in the array
i.​ Finally display the largest and second-largest number

4
4.​ Rework the program 2, especially the Hint f where if index equals maxDigit, we break from
the loop. Here we want to modify to Increase the size of the array i,e maxDigit by 10 if the
index is equal to maxDigit. This is done to consider all digits to find the largest and
second-largest number
Hint =>
a.​ In Hint f inside the loop if the index is equal to maxDigit, increase maxDigit and make
digits array to store more elements.
b.​ To do this, we need to create a new temp array of size maxDigit, copy from the current
digits array the digits into the temp array, and assign the current digits array to the temp
array
c.​ Now the digits array will be able to store all digits of the number in the array and then
find the largest and second largest number
5.​ Create a program to take a number as input and reverse the number. To do this, store the
digits of the number in an array and display the array in reverse order
Hint =>
a.​ Take user input for a number.
b.​ Find the count of digits in the number.
c.​ Find the digits in the number and save them in an array
d.​ Create an array to store the elements of the digits array in reverse order
e.​ Finally, display the elements of the array in reverse order
6.​ An organization took up an exercise to find the Body Mass Index (BMI) of all the persons in
the team. For this create a program to find the BMI and display the height, weight, BMI and
status of each individual
Hint =>
a.​ Take input for a number of persons
b.​ Create arrays to store the weight, height, BMI, and weight status of the persons
c.​ Take input for the weight and height of the persons
d.​ Calculate the BMI of all the persons and store them in an array and also find the weight
status of the persons
e.​ Display the height, weight, BMI, and weight status of each person
f.​ Use the table to determine the weight status of the person

5
7.​ Rewrite the above program using multi-dimensional array to store height, weight, and BMI in
2D array for all the persons
Hint =>
a.​ Take input for a number of persons
b.​ Create a multi-dimensional array to store weight, height and BMI. Also create an to store
the weight status of the persons
double[][] personData = new double[number][3];
String[] weightStatus = new String[number];

c.​ Take input for weight and height of the persons and for negative values, ask the user to
enter positive values
d.​ Calculate BMI of all the persons and store them in the personData array and also find
the weight status and put them in the weightStatus array
e.​ Display the height, weight, BMI and status of each person
8.​ Create a program to take input marks of students in 3 subjects physics, chemistry, and
maths. Compute the percentage and then calculate the grade as per the following
guidelines

Hint =>
a.​ Take input for the number of students
b.​ Create arrays to store marks, percentages, and grades of the students
c.​ Take input for marks of students in physics, chemistry, and maths. If the marks are
negative, ask the user to enter positive values and decrement the index
d.​ Calculate the percentage and grade of the students based on the percentage
e.​ Display the marks, percentages, and grades of each student
9.​ Rewrite the above program to store the marks of the students in physics, chemistry, and
maths in a 2D array and then compute the percentage and grade
Hint =>
a.​ All the steps are the same as the problem 8 except the marks are stored in a 2D array
b.​ Use the 2D array to calculate the percentages, and grades of the students

6
10.​Create a program to take a number as input find the frequency of each digit in the number
using an array and display the frequency of each digit
Hint =>
a.​ Take the input for a number
b.​ Find the count of digits in the number
c.​ Find the digits in the number and save them in an array
d.​ Find the frequency of each digit in the number. For this define a frequency array of size
10, Loop through the digits array, and increase the frequency of each digit
e.​ Display the frequency of each digit in the number

You might also like