0% found this document useful (0 votes)
0 views14 pages

Exercise 1 Basic Java Programs

The document contains a series of basic Java programs designed for beginners, covering fundamental concepts such as printing output, user input, arithmetic operations, and conditional statements. Each program is accompanied by explanations of key terms and sample outputs. The programs include examples like Hello World, addition of numbers, checking even or odd, and finding the largest number among inputs.

Uploaded by

madhumitha180707
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)
0 views14 pages

Exercise 1 Basic Java Programs

The document contains a series of basic Java programs designed for beginners, covering fundamental concepts such as printing output, user input, arithmetic operations, and conditional statements. Each program is accompanied by explanations of key terms and sample outputs. The programs include examples like Hello World, addition of numbers, checking even or odd, and finding the largest number among inputs.

Uploaded by

madhumitha180707
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

Exercise 1 Basic Java Programs

MOHAMED YOUSUFF (AP/CI/SCOPE, VIT)

Contents
1 Basic Java Programs – Part 1 1
1.1 Program 1: First Java Program – Hello World . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Program 2: Printing Student Details . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Program 3: Addition of Two Numbers Using User Input . . . . . . . . . . . . . . . . . 2
1.4 Program 4: Addition of Two Numbers Using Command-Line Arguments . . . . . . . . 4
1.5 Program 5: Area of a Circle Using User Input . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Program 6: Swapping Two Numbers Using User Input . . . . . . . . . . . . . . . . . 6
1.7 Program 7: Checking Whether a Number is Even or Odd Using User Input . . . . . . . 8
1.8 Program 8: Finding the Largest of Two Numbers Using User Input . . . . . . . . . . . 9
1.9 Program 9: Finding the Largest of Three Numbers Using User Input . . . . . . . . . . 10
1 BASIC JAVA PROGRAMS – PART 1

1 Basic Java Programs – Part 1

1.1 Program 1: First Java Program – Hello World

public class HelloWorld {


public static void main(String[] args) {
[Link]("Hello World");
}
}

Output

1 Hello World

Explanation of Terms

Term Explanation
public An access modifier that allows the class or method to be accessed
from outside the class.
class A Java keyword used to define a class. Every Java program is
written inside a class.
HelloWorld The class name. Since the class is declared as public, the file
name should be [Link].
static A keyword that allows the main method to execute without creating
an object of the class.
void Specifies that the method does not return any value.
main The starting point of execution in a Java program.
String[] Represents an array of strings. It is used to receive command-line
arguments.
args The variable name used to store command-line arguments.
[Link]() A predefined statement used to display output on the console and
move the cursor to the next line.
"Hello World" A string literal. The text inside double quotation marks is printed
as output.
1 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)
1.3 Program 3: Addition of Two Numbers Using User Input
1 BASIC JAVA PROGRAMS – PART 1

{ and } Braces used to mark the beginning and end of a block.


; Statement terminator used to end a Java statement.

1.2 Program 2: Printing Student Details

public class StudentDetails {


public static void main(String[] args) {
[Link]("Name: Arun");
[Link]("Department: Computer
Science");
[Link]("Year: First Year");
}
}

Output

1 Name: Arun
2 Department : Computer Science
3 Year: First Year

Explanation of Terms

Term Explanation
StudentDetails The class name chosen to represent a program that displays student-
related information.
"Name: Arun" A string literal used to display the name of the student.
"Department: A string literal used to display the department name.
Computer Science"
"Year: First A string literal used to display the year of study.
Year"

1.3 Program 3: Addition of Two Numbers Using User Input

2 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.3 Program 3: Addition of Two Numbers Using User Input
1 BASIC JAVA PROGRAMS – PART 1

import [Link];

public class AdditionInput {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int a;
int b;
int sum;

[Link]("Enter first number: ");


a = [Link]();

[Link]("Enter second number: ");


b = [Link]();

sum = a + b;

[Link]("Sum = " + sum);


}
}

Sample Output

1 Enter first number : 10


2 Enter second number : 20
3 Sum = 30

3 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.4 Program 4: Addition of Two Numbers Using Command-Line
1 BASIC Arguments
JAVA PROGRAMS – PART 1

Explanation of Terms

Term Explanation
import A keyword used to include a predefined class or package in the
program.
[Link] Refers to the Scanner class available in the [Link] package.
Scanner A predefined Java class used to read input from the keyboard.
sc An object name created for the Scanner class.
new A keyword used to create an object.
[Link] Represents the standard input stream, usually the keyboard.
int A data type used to store integer values.
a A variable used to store the first number.
b A variable used to store the second number.
sum A variable used to store the result of addition.
[Link]() Displays output without moving the cursor to the next line.
nextInt() A method of the Scanner class used to read an integer value.
= Assignment operator used to assign a value to a variable.
+ Arithmetic addition operator. It also performs string concatenation
when used with text.

1.4 Program 4: Addition of Two Numbers Using Command-Line Arguments

public class AdditionCommandLine {


public static void main(String[] args) {
int a = [Link](args[0]);
int b = [Link](args[1]);

int sum = a + b;

[Link]("Sum = " + sum);


}
}

4 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.5 Program 5: Area of a Circle Using User Input 1 BASIC JAVA PROGRAMS – PART 1

Execution Command

1 javac AdditionCommandLine .java


2 java AdditionCommandLine 15 25

Output

1 Sum = 40

Explanation of Terms

Term Explanation
AdditionCommandLine The class name used for a program that receives input values through
command-line arguments.
args[0] Represents the first command-line argument. Array indexing starts
from zero in Java.
args[1] Represents the second command-line argument.
Integer A wrapper class used to work with integer values as objects.
parseInt() A method used to convert a string value into an integer value.
javac Java compiler command used to compile a Java source file.
java Java interpreter command used to run the compiled program.

1.5 Program 5: Area of a Circle Using User Input

import [Link];

public class CircleAreaInput {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

double radius;
double area;

5 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.6 Program 6: Swapping Two Numbers Using User Input
1 BASIC JAVA PROGRAMS – PART 1

[Link]("Enter radius: ");


radius = [Link]();

area = 3.14 * radius * radius;

[Link]("Area of Circle = " +


area);
}
}

Sample Output

1 Enter radius : 5
2 Area of Circle = 78.5

Explanation of Terms

Term Explanation
CircleAreaInput The class name used for a program that calculates the area of a
circle from keyboard input.
double A data type used to store decimal values.
radius A variable used to store the radius of the circle.
area A variable used to store the calculated area of the circle.
nextDouble() A method of the Scanner class used to read a decimal value.
3.14 Approximate value of π, used in the area formula.
* Multiplication operator used to multiply numeric values.

1.6 Program 6: Swapping Two Numbers Using User Input

import [Link];

public class SwapInput {

6 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.6 Program 6: Swapping Two Numbers Using User Input
1 BASIC JAVA PROGRAMS – PART 1

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

int a;
int b;
int temp;

[Link]("Enter first number: ");


a = [Link]();

[Link]("Enter second number: ");


b = [Link]();

temp = a;
a = b;
b = temp;

[Link]("After swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
}
}

Sample Output

1 Enter first number : 10


2 Enter second number : 20
3 After swapping :
4 a = 20
5 b = 10

7 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.7 Program 7: Checking Whether a Number is Even or 1OddBASIC
UsingJAVA
User PROGRAMS
Input – PART 1

Explanation of Terms

Term Explanation
SwapInput The class name used for a program that swaps two input values.
temp A temporary variable used to store a value during the swapping
process.
temp = a Stores the original value of a in temp.
a = b Assigns the value of b to a.
b = temp Assigns the original value of a to b.

1.7 Program 7: Checking Whether a Number is Even or Odd Using User


Input

import [Link];

public class EvenOddInput {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int number;

[Link]("Enter a number: ");


number = [Link]();

if (number % 2 == 0) {
[Link]("Even Number");
} else {
[Link]("Odd Number");
}
}
}

8 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.8 Program 8: Finding the Largest of Two Numbers Using
1 BASIC
User Input
JAVA PROGRAMS – PART 1

Sample Output

1 Enter a number : 12
2 Even Number

Explanation of Terms

Term Explanation
EvenOddInput The class name used for a program that checks whether the input
number is even or odd.
number A variable used to store the input value.
if A decision-making statement used to test a condition.
% Modulus operator. It gives the remainder after division.
== Equality operator used to compare two values.
else A block that executes when the if condition is false.
number % 2 == 0 A condition that checks whether the number is divisible by 2. A
zero remainder indicates an even number.

1.8 Program 8: Finding the Largest of Two Numbers Using User Input

import [Link];

public class LargestTwoInput {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int a;
int b;

[Link]("Enter first number: ");


a = [Link]();

[Link]("Enter second number: ");

9 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.9 Program 9: Finding the Largest of Three Numbers Using
1 BASIC
User Input
JAVA PROGRAMS – PART 1

b = [Link]();

if (a > b) {
[Link]("Largest number = " + a);
} else {
[Link]("Largest number = " + b);
}
}
}

Sample Output

1 Enter first number : 45


2 Enter second number : 30
3 Largest number = 45

Explanation of Terms

Term Explanation
LargestTwoInput The class name used for a program that finds the larger value among
two input numbers.
> Relational operator used to check whether the left-side value is
greater than the right-side value.
a > b A condition that checks whether a is greater than b.

1.9 Program 9: Finding the Largest of Three Numbers Using User Input

import [Link];

public class LargestThreeInput {


public static void main(String[] args) {

10 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.9 Program 9: Finding the Largest of Three Numbers Using
1 BASIC
User Input
JAVA PROGRAMS – PART 1

Scanner sc = new Scanner([Link]);

int a;
int b;
int c;

[Link]("Enter first number: ");


a = [Link]();

[Link]("Enter second number: ");


b = [Link]();

[Link]("Enter third number: ");


c = [Link]();

if (a >= b && a >= c) {


[Link]("Largest number = " + a);
} else if (b >= a && b >= c) {
[Link]("Largest number = " + b);
} else {
[Link]("Largest number = " + c);
}
}
}

Sample Output

1 Enter first number : 25


2 Enter second number : 75
3 Enter third number : 50

11 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.9 Program 9: Finding the Largest of Three Numbers Using
1 BASIC
User Input
JAVA PROGRAMS – PART 1

4 Largest number = 75

Explanation of Terms

Term Explanation
LargestThreeInput The class name used for a program that finds the largest value among
three input numbers.
c A variable used to store the third input number.
>= Relational operator used to check whether one value is greater than
or equal to another value.
&& Logical AND operator. It returns true only when both conditions
are true.
else if A decision-making block used to test another condition when the
previous condition is false.
a >= b && a >= c A condition that checks whether a is greater than or equal to both b
and c.
b >= a && b >= c A condition that checks whether b is greater than or equal to both a
and c.

Program 10: Checking Whether a Number is Positive, Negative, or Zero

import [Link];

public class PositiveNegativeZero {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int number;

[Link]("Enter a number: ");


number = [Link]();

if (number > 0) {

12 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)


1.9 Program 9: Finding the Largest of Three Numbers Using
1 BASIC
User Input
JAVA PROGRAMS – PART 1

[Link]("Positive Number");
} else if (number < 0) {
[Link]("Negative Number");
} else {
[Link]("Zero");
}
}
}

Sample Output

1 Enter a number : -8
2 Negative Number

Explanation of Terms

Term Explanation
PositiveNegativeZero The class name used for a program that classifies a number as
positive, negative, or zero.
< Relational operator used to check whether the left-side value is
smaller than the right-side value.
number > 0 A condition that checks whether the number is positive.
number < 0 A condition that checks whether the number is negative.
Zero The output displayed when the number is neither positive nor neg-
ative.

13 Dr. Mohamed Yousuff (AP/CI/SCOPE, VIT)

You might also like