Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Java lab-2

The document outlines a lab focused on Java basic syntax, variables, and methods, with an objective to understand these concepts through practical programming tasks. It includes instructions for three Java programs: calculating the area of a hexagon, performing various operations on two integers, and comparing two numbers using relational operators. Each section provides detailed steps for creating classes, taking user input, performing calculations, and verifying results.

Uploaded by

cocic79333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java lab-2

The document outlines a lab focused on Java basic syntax, variables, and methods, with an objective to understand these concepts through practical programming tasks. It includes instructions for three Java programs: calculating the area of a hexagon, performing various operations on two integers, and comparing two numbers using relational operators. Each section provides detailed steps for creating classes, taking user input, performing calculations, and verifying results.

Uploaded by

cocic79333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab – 2

Lab Title : Java Basic Syntax, Variables, and Methods


Objective : To understand the basic syntax of Java.
To learn how to declare and use variables in Java.
To write and call methods in Java.
Prerequisites: Ensure JDK and an IDE (such as Eclipse,) are installed.
Basic knowledge of Java syntax, method, variables and arithmetic operators.
Marks: 10 marks

1. Write a Java program to compute hexagon area.


2. Write a Java program that accepts two integers from the user and prints the sum, the
difference, the product, the average, the maximum (the largest of the two integers), and
the minimum (the smallest of the two integers).
3. Write a Java program to compare two numbers using Relational Operators.

No.1 Lab Instructions


➢ Create a New Java class named “HexagonArea”.
➢ Write the Java Program:
o Import the Scanner class to take user input.
o Define the HexagonArea class and create the main method.
o Prompt the user to enter the side length of the hexagon.
o Calculate the area using the given formula.
o Display the result with appropriate formatting.
➢ Run and Test the Program
o Compile and run the program.
o Enter different values for the hexagon's side length.
o Verify the output against manual calculations.
➢ Used Math.sqrt() and Math.pow() for calculations.
➢ Used Scanner for user input and formatted output with printf().
➢ Save the code and output result in Microsoft word.
No.2 Lab Instructions
➢ Create a new Java class named “NumberOperations”.
➢ Write the Java Program
o Import the Scanner class for user input.
o Define the NumberOperations class with the main method.
o Prompt the user to enter two integers.
o Perform the following operations:
▪ Sum
▪ Difference
▪ Product
▪ Average
▪ Maximum value
▪ Minimum value
o Display the results
➢ Run and Test the Program
o Compile and run the program.
o Enter two different integers as input.
o Verify the calculations against manual computations.
➢ Save the code and output result in Microsoft word.

No.3 Lab Instructions


➢ Create a new Java class named “CompareNumbers”.
➢ Write the Java Program
o Import the Scanner class to read user input.
o Declare two integer variables to store the user-input numbers.
o Use relational operators (==, !=, >, <, >=, <=) to compare the two numbers.
o Print the results of the comparisons.
➢ Run and Test the Program
o Compile and run the program.
o Enter two different numbers and observe the results.
o Verify that all relational operators function correctly.
➢ Save the code and output result in Microsoft word.

You might also like