Object Oriented Programming Lab 1 PDF

Summary

This document is a presentation on Object Oriented Programming (OOP) and basic Java concepts. It covers topics like the introduction to OOP, the need for OOP, classes and objects, OOP concepts, and software development tools. It further extends into Java installation, packages, displaying outputs, and Hands-on exercises like Quadratic Equations and Temperature Converter.

Full Transcript

Object Oriented Programming Lab 1 Agenda Logistics Introduction to OOP Main OOP Vocabulary Getting started with IntelliJ How to install Quick start Guide Displaying outputs How t...

Object Oriented Programming Lab 1 Agenda Logistics Introduction to OOP Main OOP Vocabulary Getting started with IntelliJ How to install Quick start Guide Displaying outputs How to run / debug ? Hands-on 2 Logistics Attendance will be restricted to your section time slot. 3 OOP ? Introduction to OOP OOP is captured from real life, we always deal with different types of objects. cars, mobiles, PCs, etc… All of these are objects that have particular behavior and attributes. OOP can be defined as a programming paradigm where a software system is represented as a collection of objects that interact with each other to solve the overall task. 5 Introduction to OOP Why Do We Need OOP? There are problems in SP: Unrestricted Access 6 Introduction to OOP Why Do We Need OOP? Unrelated Functions and Data (No Real Modeling) 7 Why Do We Need OOP? Think about everything as objects of classes! 8 Classes & Objects Class Attributes A class is a kind of data type that you can Student define yourself. +Name: String +Grade: double The class defines the attributes of the object +Total() :double and the operations that are performed on/by the +Display() object. Operations (Behavior) Objects are variables of type class. Every object belongs to (is an instance of) a Object Object class. Mark Sally A program is a set of objects telling each other 9 what to do, by sending messages (Calling +Name=“Mark" +Name=“Sally" +Grade=90.0 methods). +Grade=80.0 OOP Concepts 10 Before JAVA 11 Software Development Tools Editor (IDE) Linker Where the programmer writes source converts one or several object code modules into an executable program Compiler Debugger translates the source into object steps through the program “in slow code (instructions specific to a motion” and helps find logical particular CPU) mistakes (“bugs”) Editor Compiler Source Object code code Editor Source Interpreter Linker 12 Executable Editor Source code Compiler program Object code code Introduction to Java Java is platform-independent: the same program can run on any correctly implemented Java system using an appropriate JRE or JVM. Java is object-oriented: Structured in terms of classes, which group data with operations on that data. This course mainly focuses on J2SE. 13 Installation JVM is the virtual engine and one which enables byte code support. JRE contains JVM and all the other libraries to run Java applications. It is enough to run. any Java application. JDK is a superset that comprises JVM, JRE, and the tools to develop Java applications. Its primary objective is to provide support for the build and 14 compilation. JIT Code Generator 15 Different IDEs An Integrated Development Environment is a computer software to help computer programmers develop software. The Leaders: - NetBeans - Microsoft Visual Studio - Eclipse - Visual Code - IntelliJ 16 16 How to Install IntelliJ? 17 Packages in JAVA A group of related classes. To guarantee a unique package name, use your company‘s Internet domain name (which is known to be unique) written in reverse. For example, oracle.com is a domain when written in reverse order, it turns into the package name com.oracle Packages can be nested. Standard Java Packages: java.* , javax.* 18 such as java.lang, java.util, java.net, and so on Packages A class can use all classes from its own If the same class “Date” exists in package and all public classes from other two packages and they are packages. imported in the project , I have to specify which date I want to use: To access public classes in other packages we use the key word import import java.util.*; import java.util.Date; import java.sql.*; import java.util.Date; Or we can import all classes in a package Date today; import java.util.*; Or java.util.Date deadline; 19 java.sql.Date today; Displaying outputs To Output the message we use: Print: shows value passed to it. System.out.print (" …"); System.out.print (" Hello"); Println: shows value followed by new Line System.out.println(" …"); System.out.println (" Hello"); Printf: shows value with a certain format 20 System.out.printf(….); Displaying Outputs: Printf(…) System.out.printf(“%parameter”, value); Common parameters: 'd': decimal integer 'f': decimal notation for float 'c': for a character 's': for a string. 'b': for a boolean value 🡪 "true" or "false" 'o': octal integer 'x': hexadecimal integer 'n': "%n" has the same effect as "\n". 21 Displaying outputs: Printf(...) cont’ Examples: If you didn’t write f 1. System.out.printf(“%s”, “Hello”); 🡪 Hello after the number there will be an error as any 2. String str=“hello”; decimal number is of System.out.printf(“%s”, str); 🡪hello a double type “by 3. System.out.printf(“%d”, 10); 🡪 10 default” 4. int x=10;.1 means round to System.out.printf(“%s=%d”, “x”, x); 🡪 x=10 the nearest 1 decimal number. 5. int x=1000; So.5 means round System.out.printf(“%,d”, x); 🡪 1,000 to the nearest 5 6. float y =5.365f; decimals System.out.printf(“%.1f”, y); 🡪 5.4 22 Displaying Outputs Cont’ 23 Exercise 1: HelloWorld int z=1500000; X said:"I have 1,500,000$” double y=1000.525435; String mrX="X"; Y said:"Ok Mr\X, I have 1000.525$" char currency='$'; 24 Reading Input from User To read something from the console: 1. Import the package that has the class Scanner. (The import line is to be written under the name of your package) 🡪 import java.util.Scanner; 2. Take an object from the Scanner class. 🡪 Scanner input =new Scanner(System.in) ; 3. Use the Scanner suitable method to read the next input according to its data type. e.g. int x=input.nextInt(); float f= input.nextFloat(); String s= input.next(); 25 char c = input.next().charAt(0); Exercise 2: Adding Two Numbers Read from user Import package containing scanner class Scanner Object Show message to enter Read first Firstnumber Number Same Scanner using steps with second Object number Show Sum 26 How to Debug? (Watch it Later) 27 Hands on #1: Quadratic Equation Consider the following quadratic equation: 3X2 -8X + 4 Write a program that reads X from user and shows result. Try the following values X=2 the result will be zero. X=200 the result will be 118404. X=1 the result will be -1. 10 Minutes 28 solution 29 Hands on #2: Temperature Converter Write a program to convert temperature from Fahrenheit to Celsius and vice versa. From Fahrenheit to Celsius : Celsius = ( ( Fahrenheit - 32 ) * 5 / 9 ) From Celsius to Fahrenheit : Fahrenheit = ( ( Celsius * 9 ) / 5 ) + 32 30 Hands on #2: Temperature Converter Implement two methods (functions) for conversion. Read Temperature and type to convert to from user. Display converted temperature. Test: Enter (26) and convert it to Fahrenheit which will be (78.8) 20 Minutes 31 Solution 32 Solution cont’ 33 THANK YOU

Use Quizgecko on...
Browser
Browser