0% found this document useful (0 votes)
6 views15 pages

Java Programming Basics and Examples

The document contains a series of Java programs, each with a specific aim and corresponding code. Programs include printing 'Hello World', calculating sums, checking for even or odd numbers, determining leap years, and more. Each program is accompanied by its output and was developed using Apache Netbeans 12.6.

Uploaded by

kriti3111
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)
6 views15 pages

Java Programming Basics and Examples

The document contains a series of Java programs, each with a specific aim and corresponding code. Programs include printing 'Hello World', calculating sums, checking for even or odd numbers, determining leap years, and more. Each program is accompanied by its output and was developed using Apache Netbeans 12.6.

Uploaded by

kriti3111
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

Information Technology Practical

java
PROGRAM-1
Aim:- Program for printing “ Hello World “ on screen.
Code:
package helloworldapp; public class
HelloWorldApp { public static void
main(String[] args) {

} O u t p u t : -

[Link]("Hello World!");
}
PROGRAM-2

Aim:- Program for calculating the addition of two numbers.


Code:
public class CAL
{
public static void main(String[] args)

int num1 = 5;
int num2 = 15;
int num3;
num3 = num1 + num2;
[Link]("Sum of these numbers: "+num3);

}
}
O u t p u t :
PROGRAM-3

Aim:- Program for Checking whether the given number is even or odd.
Code:-

public class IfElseExample

{
public static void main(String[] args)
{

int number=13;
if(number%2==0)

[Link]("even number");

else

[Link]("odd number");
}

} }
Output:-
PROGRAM-4

Aim:- Program for Checking whether the given year is leap or not.
Software Used:-Apache Netbeans 12.6 Code:-
public class LeapYearExample {
public static void main(String[] args) {
int year=2020; if(((year % 4 ==0) && (year % 100 !=0)) ||
(year % 400==0)) { [Link](" LEAP YEAR");
} else { [Link]("COMMON YEAR"); }
}
}
Output:-
PROGRAM-5

Aim:- Program for Checking whether the given number is prime or not.

Software Used:-Apache Netbeans 12.6 Code:- public class prime


{

public static void main(String[] args)


{
int num = 29;
int flag = 0;
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
flag = 1;
break;
}
}
if (flag==1)
[Link](num + " is a prime number.");
else
[Link](num + " is not a prime number.");
}
}
Output:-
PROGRAM-6

Aim:- Program for calculating percentage of students and check whether the student is passed or
failed.
Software Used:-Apache Netbeans 12.6

Code:-
public class NewMain

{
public static void main(String[] args)

int total=400;

int g=120;
int percentage=0;

percentage=(g/total)*100;
[Link](percentage);

if (percentage>= 40)

[Link]("PASSED");

else

[Link]("FAILED");

} }}

Output:-
PROGRAM:-7

Aim:- Program for printing Table of 5 on screen.


Software Used:-Apache Netbeans 12.6

Code:
public class table
{
public static void main(String[] args)

int num = 5;
for(int i = 1; i <= 10; ++i)
{

[Link]("%d * %d = %d \n", num, i, num * i);

}
}
O u t p u t :
PROGRAM-8

Aim:- Program for finding out day of the week based on the number provided .
Software Used:-Apache Netbeans 12.6
Code:-
public class days
{

public static void main(String[] args) {

{
int today = 5; String day = ""; switch (today) { case 1: day = "Monday"; break; case 2:
day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day =
"Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break;
case 7: day = "Sunday"; break; default: day = "Incorrect Day"; break; }
[Link] (day);

}
}

Output:
PROGRAM-9

Aim: write a program for showing functionality of Array.


Code:
public class arrays
{
public static void main(String[] args)
{
int[] age = {12, 4, 5, 2, 5};
[Link]("Accessing Elements of Array:");
[Link]("First Element: " + age[0]);
[Link]("Second Element: " + age[1]);
[Link]("Third Element: " + age[2]);
[Link]("Fourth Element: " + age[3]);
[Link]("Fifth Element: " + age[4]);
}
}

Output:-
PROGRAM-10

Aim:- Program for calculating are of rectangle by using user defined method .
Software Used:-Apache Netbeans 12.6
Code:-
public class area
{
static double rectangle_area (double length, double breadth)
{

return (length * breadth);


}
public static void main(String[] args)
{

double a = 0;

a = rectangle_area(45.5, 78.5);

[Link]("Area of the rectangle = "+ a);

}
Output:-

:
PROGRAM-11

Aim:- Program for creating a class and object in Java .


Software Used:-Apache Netbeans 12.6
Code:-
public class class1
{
int id=101;
String name="Ram";

public static void main(String args[])

class1 s1=new class1();

[Link]([Link]);

[Link]([Link]);

}
Output:
PROGRAM-12

Aim:- Program for creating a class and object for printing name and age of person in Java .
Software Used:-Apache Netbeans 12.6
Code:-
public class empname
{
String fname = "John";
String lname = "Doe";
int age = 24;
public static void main(String[] args)

empname myObj = new empname();


[Link]("Name: " + [Link] + " " + [Link]);
[Link]("Age: " + [Link]);
}
}
Output:-
PROGRAM-13

Aim:- Program for converting uppercase string in lower case string in Java .
Software Used:-Apache Netbeans 12.6
Code:-
public class string1
{

public static void main(String[] args) {

String txt = "Hello World";


[Link]([Link]());
[Link]([Link]());

}
}
Output:-
PROGRAM-14

Aim:- Program for concatenation of two strings in Java .


Software Used:-Apache Netbeans 12.6
Code:-

public class concat


{

public static void main(String[] args)

String first = "Hello ";

[Link]("First String: " + first);


String second = "World";
[Link]("Second String: " + second);

String joinedString = [Link](second);

[Link]("Joined String: " + joinedString);


}
}
Output:-
PROGRAM-15

Aim:- Program for checking whether the given string is palindrome in Java .
Software Used:-Apache Netbeans 12.6
Code:-
public class palindrome
{
public static void main(String args[])

int r,sum=0,temp;
int n=454;

temp=n;

while(n>0)

r=n%10;
sum=(sum*10)+r;
n=n/10;

}
if(temp==sum)

[Link]("palindrome number ");

else

[Link]("not palindrome");

}
}
Output:-

Common questions

Powered by AI

The Java program for checking if a number is prime uses a for loop to iterate from 2 to half of the number, num/2. For each iteration, it checks if the current iterator, i, divides the number without leaving a remainder using the modulus operator. If such a division exists, it sets a flag to 1 and breaks out of the loop, indicating that the number is not prime. A conditional statement follows to print the result based on the flag value: 'is a prime number' or 'is not a prime number' .

The area calculation program utilizes a static method, rectangle_area, which takes length and breadth as parameters and returns their product, representing the rectangle's area. This method is crucial as it encapsulates the logic for area computation, allowing it to be reused wherever needed without the need to duplicate code. The method is called from the main method, demonstrating separation of concerns and illustrating how methods encapsulate functionality, promote code reusability, and improve readability .

The palindrome-checking program checks numeric palindrome status by reversing its digits and comparing to the original. An improvement could involve using a StringBuilder to reverse a string representation of the number, which is more efficient. Another approach is to use a two-pointer technique, comparing digits from the start and end of the string, moving inward, improving readability and maintainability by using higher-level abstractions that align with Java best practices for string handling and algorithm clarity .

The Java program for converting uppercase to lowercase makes use of the toLowerCase method of the String class to demonstrate string manipulation functions. This operation is important as it shows Java's capability to handle strings and modify their characteristics, a fundamental requirement in text processing tasks such as case normalization, enabling consistent data input, search, and storage .

The 'Hello World' program in Java uses a package declaration, a public class, and a main method, which are fundamental elements of Java syntax and structure. The package declaration indicates the namespace in which the class is stored. The class is declared as public, meaning it can be accessed by any other class. The main method, public static void main(String[] args), is the entry point of a Java program and is necessary for the program's execution. Inside the main method, the System.out.println command prints 'Hello World!' to the console, demonstrating output operations in Java .

The Java program determines if a year is a leap year by checking two conditions: if a year is divisible by 4 but not 100, or if it is divisible by 400. This is implemented using an if statement with a logical expression: if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)). If the year satisfies either condition, it is a leap year and 'LEAP YEAR' is printed; otherwise, 'COMMON YEAR' is printed .

The Java program demonstrates string concatenation using the concat method, highlighting a significant aspect of string handling in Java: its immutability. When strings are concatenated, the original strings remain unchanged; a new string object containing the concatenated result is created. This immutability is managed by the language's internal design, ensuring thread safety and consistency. The program exemplifies using string operations without altering original data, crucial for understanding memory and resource management in Java .

The Java program for printing a multiplication table uses a for loop to iterate from 1 to 10, which are typical table bounds. For each iteration, it calculates the product of the loop variable and the constant number, 5, with System.out.printf used to format and print each equation in the form '5 * i = product'. This demonstrates how loops efficiently handle repetitive tasks by executing the same code block multiple times with changing values .

The Java program for demonstrating array functionality showcases several operations: declaration, initialization, and accessing elements. An integer array named 'age' is declared and initialized with specific values. The program then accesses and prints each element using its index in a series of System.out.println statements. This demonstrates how arrays store collections of data of the same type and how elements are accessed via zero-based indexing .

The program for calculating a student's percentage contains a critical error: the percentage calculation integer divide resulting in zero. The expression percentage=(g/total)*100 should use float division, like percentage = (float) g / total * 100, to prevent integer division. This is because both g and total are integers, resulting in division truncation. With integer division, percentages under 1 become 0, affecting the pass/fail evaluation, potentially leading to inaccurate results .

You might also like