0% found this document useful (0 votes)
13 views2 pages

Java Programming Practice for IGCSE

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)
13 views2 pages

Java Programming Practice for IGCSE

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 Programming Practice Sheet

Class Assignment

IGCSE Grade 10

Date : 24-11-2023

a. Write a short java program using a while loop to allow the user to input
the weight of sacks of rice and to count the number of sacks the user has
input. When the user types in ‘-1’ this should stop the process and the
program should then output the number of sacks entered and the total
weight.

b. Develop a Java program that computes and displays the highest, lowest,
and average scores for 20 students in six subjects.
The program should prompt the user to enter their name, subject and
the scores for each subject, with each subject comprising five tests, each
scored out of 100.
The program should ensure that the entered scores fall within the valid
range of 0 to 100.
The program will display the student name ,highest, lowest, and average
scores for each subject across all five tests.

c. Write a java code to implement a linear search to find the record for
the specified employee ID number in the array EmployeeRecords[10].
The algorithm should efficiently search the unsorted list of employee ID
numbers and return the index of the matching record or inform the user
that the record was not found.

d. Write a java code to implement a bubble sort algorithm to sort the


product records in chronological order by their name. The code should
efficiently sort the list of product records and ensure that the records
are arranged in ascending order based on their product names.
e. Write a java code to write a line of text to a file and read it back from
the file.

f. Write and test a short program in your chosen programming language


to:

input two integers a and b and then find a MOD b and a DIV b
create a random integer between 100 and 300.

g. write and run a short program to input your full name into a variable,
MyName, find the length of your name, extract the first three
characters of your name and display your name in upper case and in
lower case.

h. Write a java program to use a nested FOR loop to output the numbers 1
to 5 twenty times.

Common questions

Powered by AI

The bubble sort algorithm repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the list is sorted. For sorting product records by product name, the algorithm compares the alphabetical order of product names and rearranges the records in ascending order. Although simple to implement, bubble sort has a time complexity of O(n^2), making it less efficient for large lists .

Bubble sort is less efficient due to its quadratic time complexity (O(n^2)), which requires multiple passes through the data for large lists. More efficient alternatives include quicksort, mergesort, and heapsort, which have better average-case time complexities (O(n log n)) and perform faster due to fewer comparisons and efficient data partitioning or merging techniques .

A linear search algorithm in Java iterates through each element in the EmployeeRecords array to find a specified employee ID. If the ID is found, the algorithm returns its index. If the algorithm traverses the entire array without finding the ID, it informs the user that the record was not found. One limitation of linear search is its inefficiency with large datasets, as it potentially requires checking each element sequentially until a match is found .

To handle file operations in Java, use classes from java.io package. For writing a line of text to a file, create a FileWriter object and use it to output text to the file. To read the text back, use a FileReader or BufferedReader object to stream the file's contents back into the program. These operations must handle exceptions like IOException to ensure errors are managed properly during file access .

When validating score inputs, ensure all entries fall within the specified range (0 to 100 for scores) to maintain data integrity. Implement checks to reject invalid entries and prompt for re-entry until a valid score is provided. This validation prevents calculation errors, such as incorrect averages, and ensures reliable performance of statistical analyses within the program .

Nested loops can be employed in Java to repeat outputs by placing one loop inside another. For instance, a nested FOR loop can output numbers 1 to 5 twenty times by having an outer loop execute the inner loop's sequence repeatedly. The inner loop runs five iterations from 1 to 5, while the outer loop controls the number of times this sequence is printed .

To calculate and display the highest, lowest, and average scores for multiple students in Java, a program should prompt users to input student names, subjects, and scores for each test. Each subject consists of five tests scored out of 100. The program should aggregate the scores to identify the highest and lowest scores, then compute the average by summing the scores and dividing by the number of tests. Input validation is crucial, as it ensures scores fall within the 0 to 100 range to prevent erroneous data from affecting the calculations .

A Java program can use a while loop to continuously prompt the user to input data, such as the weight of sacks of rice, and keep a tally of the number of entries made. The loop will terminate when a specific sentinel value, '-1' in this case, is entered. This approach allows the program to accumulate both the total number of inputs and the cumulative sum of the entered weights, which can then be displayed to the user once the loop exits .

In Java, you can obtain the length of a string using the length() method, extract a substring using substring(), and change string format using toUpperCase() and toLowerCase() methods. For example, inputting a name into a variable, you can find its length, extract specific portions, such as the first three characters, and convert the string to uppercase or lowercase .

In Java, the modulus of two integers 'a' MOD 'b' can be calculated using the '%' operator, while integer division 'a' DIV 'b' utilizes the '/' operator. To generate a random integer within a specified range, such as between 100 and 300, use the Random class or Math.random(), scaling the output to match the desired range, e.g., Random.nextInt(201) + 100 .

You might also like