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

Java Exercise For Beginners: How To Accept Input From The Keyboard?

1. The document provides exercises for Java beginners to practice writing basic Java programs. These include programs to print text, perform mathematical operations on user-input numbers, find the largest of three numbers, use control flow statements like if/else and for loops, and overload methods. 2. It also provides instructions on using the Scanner class to accept user input and importing packages like java.util for generating random numbers. 3. Key concepts covered include methods, method overloading, control flow, basic math operations, and using classes. The exercises allow learners to practice these foundational Java concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Java Exercise For Beginners: How To Accept Input From The Keyboard?

1. The document provides exercises for Java beginners to practice writing basic Java programs. These include programs to print text, perform mathematical operations on user-input numbers, find the largest of three numbers, use control flow statements like if/else and for loops, and overload methods. 2. It also provides instructions on using the Scanner class to accept user input and importing packages like java.util for generating random numbers. 3. Key concepts covered include methods, method overloading, control flow, basic math operations, and using classes. The exercises allow learners to practice these foundational Java concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Exercise for Beginners

1. Write a java program which displays Hello Students

2. Write a java program which displays the following multiple lines of text.
WELCOME
TO
JAVA
3. Write a Java program of two integers and then print the sum (addition), multiply, subtract,
divide and remainder of two numbers where the value of two numbers are x=10 and y=5

How to accept input from the keyboard?


 A useful class that handles input from a user is called the Scanner class.
 The Scanner class can be found in the java.util library,where util package is
used for generating random-number. (import java.util.Scanner;)
 In order to use a package you use the import keyword
4. Write a Java program of two integers and then print the sum (addition), multiply, subtract,
divide and remainder of two numbers where the value of two numbers are given from
outside or given by user.

1|P ag e
5. Write a java program which print the area and circumference of a circle, given its radius. (Radius = 2)

Output

6. Check whether a number is even or odd using if...else statement

2|P ag e
Example 2: Check whether a number is even or odd
using ternary operator
import java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

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


int num = reader.nextInt();

String evenOdd = (num % 2 == 0) ? "even" : "odd";

System.out.println(num + " is " + evenOdd);

}
}

7. Write a java program that displays the largest integer numbers of three numbers

3|P ag e
8. Write a java program that displays the largest number of three integer numbers when those
numbers are given by a user or from a keyboard.

Output

9. Write a java program which illustrates a for loop statement

public class LoopW


{
public static void main(String[] args)
{
for(int i = 0; i <= 5; i++)
System.out.println(i);
}
}

4|P ag e
Exercise

Java program to calculate Maximum and Minimum of two numbers entered by user in console.

Using Math extension a method form of max(x, y)

5|P ag e
Note:-

Java.util Package.

It contains the collections framework, legacy collection classes, event model, date and time
facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-
number generator, and a bit array).

6|P ag e
Method and Method overloading Exercise
Class MethodOverload includes two overloaded versions of method square— one that calculates the square
of an int (and returns an int) and one that calculates the square of a double (and returns a double). Although
these methods have the same name and similar parameter lists and bodies, think of them simply as different
methods. It may helptothinkofthemethodnamesas“squareofint”and“squareofdouble,”respectively.

7|P ag e
Exercise for Method overlaoding by creating a class for a method.

Output

8|P ag e

You might also like