













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A series of java programming lab tasks designed to introduce fundamental programming concepts. The tasks cover basic arithmetic operations, geometric calculations, and data input and output. Each task includes a clear problem statement, code implementation, and expected output, providing a practical learning experience for beginners in java programming.
Typology: Lab Reports
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Department of creative technologies Course Software construction and development Instructor Sir Naseer Jan Name: Laraib kanwal Rollno: Lab Task# Date of submission:03- 10 - 2024
Task 1): Radius, Diameter, and Area of a Circle Write a Java program to calculate the radius, diameter, and area of a circle from a given value of the circumference of a circle. The circumference of the circle is 62.8580. Your program computes and displays the radius, diameter, and area of a circle. CODE package Circle; /** *@author laraib kanwal
Task 2): Addition, multiplication, division, and Subtraction Write a Java program that takes five integer values from the user and perform basic arithmetic along with average calculation. Code package arithmeticoperations; /**
// Prompt the user to enter five integer values System.out.println("Enter five integer values:"); // Loop through to get user input for 5 numbers // This loop runs 5 times (once for each number) for (int i = 0; i < 5; i++) { System.out.print("Enter number " + (i + 1) + ": "); // Asking for input numbers[i] = scanner.nextInt(); // Storing the entered number in the array } // Initializing variables for different arithmetic operations int addition = 0; // Stores the sum of all numbers int multiplication = 1; // Stores the product of all numbers int subtraction = numbers[0]; // Starts subtraction from the first number double division = numbers[0]; // Starts division from the first number // Loop through the numbers to perform addition, multiplication, subtraction, and division for (int i = 0; i < 5; i++) { // Adding the current number to the total sum addition += numbers[i]; // Multiplying the current number with the total product multiplication *= numbers[i];
// Close the scanner to avoid resource leaks scanner.close(); } } Output Task 3): Light Year Calculation Write a JAVA program that converts light year to miles, kilometers, and meters. Code package lightyearconversion;
Task 4): BMI Index Write a Java program that asks the user to input mass in pounds and heights in inches, and calculate body mass index (BMI). Code package bmicalculator; /**
// Ask the user to enter mass in pounds System.out.print("Enter your weight (mass) in pounds: "); double massInPounds = scanner.nextDouble(); // Ask the user to enter height in inches System.out.print("Enter your height in inches: "); double heightInInches = scanner.nextDouble(); // Convert mass from pounds to kilograms double massInKilograms = massInPounds * 0.453592; // Convert height from inches to meters double heightInMeters = heightInInches * 0.0254; // Calculate BMI using the formula double bmi = massInKilograms / (heightInMeters * heightInMeters); // Display the calculated BMI System.out.println("Your Body Mass Index (BMI) is: " + bmi); // Close the scanner object scanner.close(); }
// Create a scanner object to take input from the user Scanner scanner = new Scanner(System.in); // Ask the user to enter the first number (dividend) System.out.print("Enter the first number (dividend): "); int dividend = scanner.nextInt(); // Ask the user to enter the second number (divisor) System.out.print("Enter the second number (divisor): "); int divisor = scanner.nextInt(); // Calculate the quotient and remainder int quotient = dividend / divisor; // Integer division to get the quotient int remainder = dividend % divisor; // Modulus operator to get the remainder // Display the results System.out.println("Quotient: " + quotient); // Displaying the quotient System.out.println("Remainder: " + remainder); // Displaying the remainder // Close the scanner object scanner.close(); } }
Output Task 6) Write a Java program that asks the user to input three integer values, computes the cube of each number, and then finds their average. Code package cubeaveragecalculator; /**
System.out.println("Cube of " + firstNumber + ": " + cube1); System.out.println("Cube of " + secondNumber + ": " + cube2); System.out.println("Cube of " + thirdNumber + ": " + cube3); System.out.printf("Average of cubes: %.2f%n", average); // Formatting the average to two decimal places // Close the scanner object scanner.close(); } } Output Task 7) Write a Java program that takes two inputs from the user and calculates the square root of the sum of their squares. In this program, the user inputs values for 'a' and 'b', and then the square root of the sum of their squares is calculated and displayed. Code package squarerootofsumofsquares; /**
// Check if the ASCII code is valid (0-127) if (asciiCode >= 0 && asciiCode <= 127) { // Convert the ASCII code to character char character = (char) asciiCode; // Display the corresponding character System.out.println("Character for ASCII code " + asciiCode + " is: " + character); } else { System.out.println("Invalid ASCII code. Please enter a value between 0 and 127."); } } // Close the scanner object scanner.close(); } } Output