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

Java Programming Exercises Guide

The document outlines a series of programming exercises in Java, focusing on basic functions and control structures. Tasks include calculating averages, sums of odd numbers, determining the greater of two numbers, and checking voting eligibility based on age. Additional challenges involve creating loops, counting user inputs, calculating powers, finding the Greatest Common Divisor, and generating a Fibonacci series.

Uploaded by

Prachi Joshi
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 views11 pages

Java Programming Exercises Guide

The document outlines a series of programming exercises in Java, focusing on basic functions and control structures. Tasks include calculating averages, sums of odd numbers, determining the greater of two numbers, and checking voting eligibility based on age. Additional challenges involve creating loops, counting user inputs, calculating powers, finding the Greatest Common Divisor, and generating a Fibonacci series.

Uploaded by

Prachi Joshi
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 - Introduction to Programming

Exercise 1
Questions
[Link] 3 numbers from the user & make a function to print their average.
[Link] a function to print the sum of all odd numbers from 1 to n.
[Link] a function which takes in 2 numbers and returns the greater of those two.
[Link] a function that takes in the radius as input and returns the circumference of a circle.
[Link] a function that takes in age as input and returns if that person is eligible to vote or not. A person of age > 18 is eligible to
vote.
[Link] an infinite loop using do while condition.
[Link] a program to enter the numbers till the user wants and at the end it should display the count of
positive, negative and zeros entered.
[Link] numbers are entered by the user, x and n. Write a function to find the value of one number
raised to the power of another i.e. xn.
[Link] a function that calculates the Greatest Common Divisor of 2 numbers. (BONUS)
[Link] a program to print Fibonacci series of n terms where n is input by user :
0 1 1 2 3 5 8 13 21 .....
In the Fibonacci series, a number is the sum of the previous 2 numbers that came before it.
(BONUS)

Common questions

Powered by AI

When designing a function to find the larger of two numbers, ensure the function can accept various data types such as integers and floating points. Comparison operations should handle potential precision errors inherent in floating-point arithmetic. Including input validation and error handling to notify if incomparable types are passed is also important for robust operation .

To write a function to count positive, negative, and zero values, iterate over the list of user inputs, maintaining separate counters for each category. As each input is read, check its value and increment the relevant counter. This approach ensures O(n) complexity for the counting operation, where n is the number of inputs. It must also handle negative input efficiently and validate inputs to manage non-numeric entries properly .

To write a function that calculates the average of three numbers, first take the three numbers as input parameters. Then, sum the numbers and divide the result by three to get the average. Considerations include ensuring the function handles different numerical data types correctly and accurately checks for the division by zero error, though unlikely, if implemented with rigid input constraints .

To calculate the circumference of a circle, the function takes the radius as an input and applies the formula 2 * π * radius. It’s essential to use the mathematical constant π from a reliable library for precision (e.g., Math.PI in Java). Consider handling edge cases like non-positive radius input to ensure the program’s robustness against incorrect data types .

Choosing between recursive and iterative approaches in calculating powers involves several factors such as stack space, execution speed, and readability. Recursive methods can be more elegant and concise but risk stack overflow for large n. Iterative methods are typically more memory-efficient and faster due to the absence of recursion overhead. Understanding these trade-offs is important for performance and reliability when implementing power functions .

Implementing a method to continuously receive user input can involve a loop that keeps asking for input until the user decides to stop, such as by entering a specific keyword. A 'do-while' loop is suitable for this purpose as it ensures the loop runs at least once. Challenges include handling user input errors, ensuring responsive exit conditions, and managing large inputs or input frequencies that could impact performance .

To determine if a person is eligible to vote based on their age, you can write a function that takes the age as an input parameter and checks whether it is greater than 18. The function would return a boolean value indicating eligibility. Key programming considerations include ensuring accurate input validation to handle different types of data input and using a clear conditional statement to improve code readability .

The Greatest Common Divisor (GCD) is important for simplifying fractions and finding integer linear combinations. It can be efficiently calculated using the Euclidean algorithm, which has a time complexity of O(log(min(a, b))) due to its iterative reduction of the problem size. The Euclidean algorithm involves repeatedly replacing the larger number by its remainder when divided by the smaller one until one of them becomes zero, at which point the other is the GCD .

The naive computation of the Fibonacci series has a time complexity of O(2^n) due to its recursive nature, where each calculation calls for two previous terms. This can be optimized to O(n) by storing previous results in an array (dynamic programming) to prevent redundant calculations, or by using an iterative approach which scales linearly by directly summing the last two computed terms for each new term .

An infinite loop can be implemented using a 'do-while' loop that always evaluates to true, such as 'do { //code } while(true);'. This ensures repeated execution of a code block until externally interrupted. Use cases for infinite loops include waiting for specific external events, running servers that check for incoming requests, or real-time systems that need continuous monitoring. It is critical to include appropriate interrupt handling to prevent resource exhaustion and ensure system stability .

You might also like