OOP Review 2024 PDF
Document Details
Uploaded by SumptuousMagnesium9351
2024
AQA
Tags
Summary
This document is a review of Object-Oriented Programming (OOP) concepts, focusing on Java. It covers introductory topics and control structures like loops and conditional statements.
Full Transcript
&;)&\QA;)S\JJ\M Uls dxal yoy padle OOP IT232 - CS230 - DS230 "l daal jall g8 5 Cladlll g Y (adldl” Ao glaall 5 A gl A0S o ) https://t.me/computingg I paal g dlae) Telegram: @mimbaj @otfhh91...
&;)&\QA;)S\JJ\M Uls dxal yoy padle OOP IT232 - CS230 - DS230 "l daal jall g8 5 Cladlll g Y (adldl” Ao glaall 5 A gl A0S o ) https://t.me/computingg I paal g dlae) Telegram: @mimbaj @otfhh91 aSaled (pa U guati Y sema i e m il INDEX Week 3: Introduction to Java applications | Week 4: Introduction to Java applications | Week 5: Control Statements | Week 6: Control Statements |l Week 7+8: Methods MIDTERM PART DONE Week 10: Arrays Week 11+12:Classes and Objects Week 13: Inheritance & Polymorphism Week 14: Strings and Characters Week 15: Recursion Week 3: Introduction to Java applications | Decl \ Class 1 // Fig. 2.1: We\comel.java 2 // Text-printing program. 3 - Declaring a public class WWelcomel / Method // main method begins execution of Java jop/hcat\on ZSea~ow E{ blic static void main(String[] args)” / Method Cla: Body [ System.out.printin@¥elcome to Java Programming!®); ‘ Body } // end method main \ J } 7/ end class Welcomel Fig. 2.1 | Text-printing program. (Part I of 2.) Writing First Program in J#va » Compiling Your First Java Application * javac Welcomel.java « If no errors, welcomel.class created * Has bytecodes that represent application * Bytecodes passed to Java interpreter * Executing the Welcome1 Application * java Welcome1l « Interpreter loads. c1ass file for class We'lcomel «.class extension omitted from command [ —_— o = Youype ths « Interpreter calls method main theappicaion P — The pogam ouputs o th seen Welcome 18){ System.out.print("Age is greater than 18"); } } } Reference: httns://wviw.iavatnoint.com/iava-if-else If....Else * Example: public class IfElseExample { public static void main(String[] args) { //defining a variable int number=13; //Check if the number is divisible by 2 or not if(number%2==0){ System.out.printIn("even number"); Jelse{ System.out.printin("odd number"); } } } Reference: httos://www.iavatooint.com/iava-if-else Nested If Statement * Java Code if (studentGrade >= 90) System.out.printin("A"); Else if (studentGrade >= 80) System.out.printin("B"); Else if (studentGrade >= 70) System.out.printin("C"); Else if (studentGrade >= 60) System.out.printin("D"); Else System.out.printin("F"); Nested If Statement « Dangling-else Problem * To force the nested if...else statement to execute as it was originally intended. switch(expression){ case valuel: //code to be executed; break; //optional case value2: //code to be executed; break; //optional Reference: https://www.javatpoint.com/iava-switch Switch.....Case public class SwitchExample {. Example: public static void m.ain(strin'g[] args) { ) ) //Declaring a variable for switch expression int number=20; //Switch expression ~ switch(number]{ //Case statements case 10: System.out.printin("10"); break; case 20: System.out.printin("20"); break; case 30: System.out.printin("30"); break; //Default case statement j default:System.out.printIn("Not in 10, 20 or 30"); } Refarance: httne:/fwnwa iavatnaint cam fiava-cwitch Switch.....Case * The Java switch statement is fall-through. It means it executes all statements after the first match if a break statement is not present. public class SwitchExample2 { public static void main(String[] args) { int number=20; //switch expression with int value switch(number){ //switch cases without break statements case 10: System.out.printin("10"); case 20: System.out.printin("20"); case 30: System.out.printin("30"); default:System.out.printIn("Not in 10, 20 or 30"); Not in 10, 20 or 30 Refarence: httne:/fananw iavatnnint ram fiava-cwitch Switch.....Case * Example: public class Test { public static void main(String args[]) char grade = 'C’; switch (grade) case 'A' : System.out.println("Excellent!"); break; e case 'C' : System.out.println("Well done"); break; 'D' : System.out.println("You passed"); 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); Conditional Operator (?: ‘ * Example: Reference: https://www.w3schoals.in/java-tutorial/operators/conditional/ Conditional Operator (?: ‘ * Example: public class LargestNumberExample { public static void main(String args[]) { int x=69; int y=89; intz=79; int largestNumber= (x >y) ? (x >z ?x:2): (y>z?y:2); System.out.printIn("The largest numbers is: "+largestNumber); Reference: https://www.javatpoint.com/conditional-operator-in-java Week 6: Control Statements Il For / While Loops ‘ the numberof teraions s fixed T - do-whiie leep hehe Goda numberof teratons1 loop 1o xecuie s ot af fed,east andnce we want Reference: https://wwwjavatpoint.com/java-for-loop For / While Loops ' int counter = 1; // declare and initialize control variable while (counter