0% found this document useful (0 votes)
3 views7 pages

Java Practical Manual

The document is a practical manual for Java programming, outlining step-by-step instructions for creating various Java applications using the NetBeans IDE. It covers topics such as printing messages, storing text data, calculating percentages, using conditional statements, and implementing loops and arrays. Additionally, it includes examples of user-defined methods and basic mathematical operations.

Uploaded by

alkullu.1989
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)
3 views7 pages

Java Practical Manual

The document is a practical manual for Java programming, outlining step-by-step instructions for creating various Java applications using the NetBeans IDE. It covers topics such as printing messages, storing text data, calculating percentages, using conditional statements, and implementing loops and arrays. Additionally, it includes examples of user-defined methods and basic mathematical operations.

Uploaded by

alkullu.1989
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 Practical Manual

1. Write a simple Java program that prints a Welcome Message “Hello World” on the screen.

Step 1: Double click the Net Beans icon on your computer to start Net Beans. It will open the Net Beans IDE.
Step 2: In the IDE, click File> New Project (Ctrl + Shift + N).
Step 3: In the New Project dialog box that appears, under the Categories list, select Java and under the
Projects list select Java Application. Click on Next to create a new Java Application Project.
Step 4: In the New Java Application dialog box that appears, in the Project Name field type a name for the
Project (type your name as project name). Click on Finish to finish creating the Java Application Project.
Step 5: On the left side of the NetBeans IDE, under the Projects tab > Source Packages a source code file
called yourname.java has been created. On the right side of the IDE is the code editor window where the
Java program code in the yourname.java file is displayed. Write the following code to display “Hello World”.

public static void main (String[] args) {


System.out.println("Hello World”);
}

Step 6: In the IDE toolbar, click on Run > Run Main Project (F6) or Run >Run File (Shift + F6) to execute the
Java Program.

Step 7.: In the IDE toolbar, click on File > Save (Ctrl + S) or File > Save All (Ctrl + Shift + S) to save the
yourname.java program. NetBeans has a default Compile on Save feature, you do not have to compile your
program manually, it is compiled automatically when you save it.

2. Write a java program to store text data.

Step 1: Create a new Class file within your name package (project that was created in the 1st program). Click
on File > New File (Ctrl + N) to create a new file.
Step 2: In the New File dialog box that appears, under the Categories list, select Java and under the File
types list select Java Class. Click on Next to create a new Java Class file.
Step 3: In the New Java Class dialog box that appears, type a name in the Class Name text box “Greetings”.
Click on Finish.
Step 4: In the Code Editor Window and type the code:
public class Greetings {
public static void main(String[]args){
var firstname = "yourfirstname";
var lastname = "yourlastname";
System.out.println("Hello " +firstname+ " " +lastname);
}

Step 5: Click Run> Run File (Shift + F6) to execute the program.
3. Write a java program to calculate percentage.

Step 1: Click on File > New File (Ctrl + N) to create a new file.
Step 2: In the New File dialog box that appears, under the Categories list, select Java and under the File
types list select Java Class. Click on Next to create a new Java Class file.
Step 3: In the New Java Class dialog box that appears, type a name in the Class Name text box
“PercentageCalculator”. Click on Finish.
Step 4: In the Code Editor Window under the Percentage Calculator. Java tab and type the code:
public static void main (String[] args) {
int totalmarks = 400;
double marksobtained = 346;
double percentage;
percentage = (marksobtained/totalmarks)*100;
System.out.println("Student1's Percentage = "+percentage);
}
Step 5: Click Run> Run File (Shift + F6) to execute the program.

4. Write a java program in NetBeans to calculate percentage by entering the marks obtained.
Step 1: From the above java program, open ‘PercentageCalculator’ and type the following code in the code
editor.
import java.util.Scanner;
public class PercentageCalculator {
public static void main (String[] args) {
// create an object of Scanner class
Scanner user_input=new Scanner(System.in);
System.out.print("Enter the marks obtained by the student: ");
double marksobtained = user_input.nextDouble();
System.out.print("Enter the total marks: ");
int totalmarks = user_input.nextInt();
double percentage;
percentage = (marksobtained/totalmarks)*100;
System.out.println("Student's Percentage = "+percentage);
}
Step 2: Click Run> Run File (Shift + F6) to execute the program.
5. Write a java program in NetBeans to execute ifElse statement. If the percentage secured by the student is
greater than or equal to 40%, we will declare the student as passed.

Step 1: From the above java program, open ‘PercentageCalculator’ and add the following code in the code
editor.
System.out.println("Student's Percentage = "+percentage);
if (percentage >= 40) {
System.out.println ("PASSED");
}
else {
System.out.println("FAILED");
}

6. Write a java program in NetBeans to execute Switch statement. To Make a Simple Calculator Using switch.

import java.util.Scanner;
public class Switch {
public static void main (String[]args){
char operator;
double number1, number2, result;
Scanner input = new Scanner(System.in);
System.out.print("Choose an operator: + , - , * or / :");
operator = input.next().charAt(0);
System.out.print("Enter the first number: ");
number1 = input.nextDouble();
System.out.print("Enter the second number: ");
number2 = input.nextDouble();

switch (operator) {
// performs addition between numbers
case '+':
result = number1 + number2;
System.out.println(number1+ " + " +number2+ " = " +result);
break;
// performs subtraction between numbers
case '-':
result = number1 - number2;
System.out.println(number1+ " - " +number2+ " = " +result);
break;
// performs multiplication between numbers
case '*':
result = number1 * number2;
System.out.println(number1+ " * " +number2+ " = " +result);
break;
// performs division between numbers
case '/':
result = number1 - number2;
System.out.println(number1+ " / " +number2+ " = " +result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}

7. Write a java program in NetBeans to execute While statement - a program to print the squares of numbers
from 1 to 10.

a. public class While {


public static void main (String[ ] args) {
int number=1;
while (number <= 10) {
System.out.print ("Square of " + number);
System.out.println (" = " + number*number);
++number; } }
b. import java.util.Scanner;
public class While {
public static void main (String[ ] args) {
int number;
Scanner input = new Scanner(System.in);
System.out.print(“Enter any number between 1-10: ”);
number = input.nextInt();
while (number <= 10) {
System.out.print ("Square of " + number);
System.out.println (" = " + number*number);
++number; } }

8. Write a java program in NetBeans to execute DoWhile statement - a program to print the squares of numbers
from 1 to 10.

a. public class DoWhile {


public static void main (String[ ] args) {
int number = 1;
do {
System.out.print ("Square of " + number);
System.out.println (" = " + number*number);
++number;
} while (number <= 10); }

b. import java.util.Scanner;
public class DoWhile {
public static void main (String[ ] args) {
int number;
Scanner input = new Scanner(System.in);
System.out.print(“Enter any number between 1-10: ”);
number = input.nextInt();
do{
System.out.print ("Square of " + number);
System.out.println (" = " + number*number);
++number;
} while (number <= 10) } }
9. Write a java program in NetBeans to execute for statement.

public class ForLoop {


public static void main (String[ ] args) {
for (int number = 1; number<= 5; ++number)
{
System.out.print("Square of "+ number);
System.out.println(" = "+ number*number);
}}

10. Write a java program to show the use of an array to print the class report card for the five students.

public class Array {


public static void main (String[ ] args) {
int[] age = {8, 12, 6, 4, 1};
System.out.println("Accessing elements of array:");
for(int i=0; i < age.length; i++) {
System.out.println(age[i]); } } }

11. Write a java program to create a User defined method.


public class UserDefinedMethod {
static double rectangle_area (double length, double breadth) {
return (length * breadth); }
public static void main(String[]args){
double a;
a = rectangle_area(34.5, 45);
System.out.println("Area of the rectangle = " +a); } }
12. Write a java program to calculate the area and perimeter of a circle.
import java.util.Scanner;
public class circle {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("Enter the radius of a circle: ");
float radius=input.nextFloat();
double area=3.14*radius*radius;
double perimeter=2*3.14*radius;
System.out.println("Area of the circle is "+area);
System.out.println("Perimeter of the circle is "+perimeter); } }

13. Write a java program to identify if the number is even or odd.

import java.util.Scanner;
public class evenodd {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print(“Enter any number: ”);
double num=input.nextDouble();
double result=num%2;
if (result==0) {
System.out.println(“The entered number is even”); }
else {
System.out.println(“The entered number is odd”); }

You might also like