OOPS_Lab_Summaries this is a whole summary of oop lab
Msaqib43
4 views
6 slides
Jun 21, 2024
Slide 1 of 6
1
2
3
4
5
6
About This Presentation
this is a whole summary of oops
Size: 32.81 KB
Language: en
Added: Jun 21, 2024
Slides: 6 pages
Slide Content
Object Oriented Programming - Lab Summaries Based on labs by Muhammad Saqib
Lab 1: Getting Familiar with Java Syntax In this lab, you will learn how to: - Declare and initialize variables in Java - Use if-else statements for decision making - Utilize loops for repetitive tasks - Understand the scope of variables Examples: 1. Print 'Hello World!' 2. Declare and print an integer variable 3. Add two decimal numbers and print the result 4. Calculate the discount of a product 5. Find the largest of two numbers using if-else 6. Compute the factorial of a number 7. Check if a number is prime
Example: Hello World! Task: Write a Java Program to print 'Hello World!' to Console. ```java class Lab_1 { public static void main(String[] args) { System.out.println("Hello World M SAQIB"); } } ```
Example: Declare and Print Integer Task: Declare and initialize an integer variable called num1 and print its value to console. ```java class Lab_1 { public static void main(String[] args) { int num1 = 10; System.out.println(num1); } } ```
Example: Add Two Decimal Numbers Task: Declare and initialize two decimal numbers, add them, and print the result. ```java class Lab_1 { public static void main(String[] args) { double num1 = 10.7; double num2 = 5.7; System.out.println(num1 + num2); } } ```
Example: Calculate Product Discount Task: Write a Java Program to find the discount of a product. ```java import java.util.Scanner; public class ProductDiscountCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the cost of the product (in rupees): "); double cost = scanner.nextDouble(); System.out.print("Enter the discount rate (in percentage): "); double discountRate = scanner.nextDouble(); double discount = (cost * discountRate) / 100; System.out.println("Discount price of the product is: " + discount + " rupees"); } } ```