Java Programming Lab Tasks: Fundamentals of Programming - Prof. Jann, Lab Reports of Software Development

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

2023/2024

Uploaded on 10/23/2024

laraib-kanwal
laraib-kanwal 🇵🇰

1 document

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Department of creative technologies
Course
Software construction and development
Instructor
Sir Naseer Jan
Name: Laraib kanwal
Rollno:221822
Lab Task#01
Date of submission:03-10-2024
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Java Programming Lab Tasks: Fundamentals of Programming - Prof. Jann and more Lab Reports Software Development in PDF only on Docsity!

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

Lab 1 Tasks

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

  • @ID: / public class Circle { /*
  • @program to calculate the radius , diameter and area of the circle / public static void main(String[] args) { // given circumference double circumference=62.8580; //calculate radius double radius=circumference /(2Math.PI); //calculate diameter

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; /**

  • @author laraib kanwal / import java.util.Scanner; public class ArithmeticOperations { /*
  • @param args the command line arguments */ public static void main(String[] args) { // Create a Scanner object for user input // 'scanner' follows camelCase naming convention Scanner scanner = new Scanner(System.in); // Declare an array to hold the five integer values // Array to store numbers from user input int[] numbers = new int[5];

// 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;

  • @author laraib kanwal / import java.util.Scanner; public class LightYearConversion { /*
  • @param args the command line arguments */ public static void main(String[] args) { // Constants for conversion factors double MILES_PER_LIGHT_YEAR = 5.88e12; // 5.88 trillion miles in one light year double KILOMETERS_PER_LIGHT_YEAR = 9.46e12; // 9.46 trillion kilometers in one light year double METERS_PER_LIGHT_YEAR = 9.46e15; // 9.46 quadrillion meters in one light year // Create a scanner object for input Scanner scanner = new Scanner(System.in); // Prompt the user to enter the number of light years System.out.print("Enter number of light years: ");

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; /**

  • @author laraib kanwal / import java.util.Scanner; public class BMICalculator { /*
  • @param args the command line arguments */ public static void main(String[] args) { // Create a scanner object to take input from the user Scanner scanner = new Scanner(System.in);

// 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; /**

  • @author laraib kanwal / import java.util.Scanner; public class CubeAverageCalculator { /*
  • @param args the command line arguments */

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; /**

  • @author laraib kanwal / import java.util.Scanner; public class SquareRootOfSumOfSquares { /*
  • @param args the command line arguments */ public static void main(String[] args) { // Create a scanner object to take input from the user Scanner scanner = new Scanner(System.in); // Ask the user to enter the first number 'a' System.out.print("Enter the value for a: "); double a = scanner.nextDouble(); // Ask the user to enter the second number 'b' System.out.print("Enter the value for b: "); double b = scanner.nextDouble(); // Calculate the sum of the squares of a and b double sumOfSquares = (a * a) + (b * b);
  • @author laraib kanwal / import java.util.Scanner; public class ASCIICharacterPrinter { /*
  • @param args the command line arguments */ public static void main(String[] args) { // Create a scanner object to take input from the user Scanner scanner = new Scanner(System.in); // Ask the user to enter the number of ASCII codes they want to input System.out.print("Enter the number of ASCII codes you want to input: "); int numberOfCodes = scanner.nextInt(); // Loop through the number of ASCII codes the user wants to input for (int i = 0; i < numberOfCodes; i++) { // Prompt the user to enter an ASCII code System.out.print("Enter ASCII code " + (i + 1) + ": "); int asciiCode = scanner.nextInt();

// 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