Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

INTERMIDIATE PROGRAMMING LESSON REVIEWER, Study notes of Biology

This study tool is about Programming Language focusing on basic programming languages

Typology: Study notes

2022/2023

Available from 08/07/2024

princess-anne-soriano-dayrit
princess-anne-soriano-dayrit 🇵🇭

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ADPROG
JAVA-METHODS
JAVA METHOD
- is a collection of statements that are
grouped together to perform an operation.
- When you call the:
“System.out.println(
)
method, for example, the system actually
executes several statements in order to
display a message on the console.
Create own methods or without return
values
Invoke a method with or without
parameters, and apply method
abstraction in the program design.
SYNTAX
public static int methodName(int a, int b)
{
//body
}
public static - modifier
int - return type
methodName- name of the method
a,b- formal parameters
int a, int b - list of parameters
METHOD CALLING
- it should be called.
- there are two ways in which a method is
called:
return a value or
returning nothing (no return value)
- the process of “method calling” is simple.
- when a program invokes a method, the
program control gets transferred to the
“called method”
- this “called method” the returns control to
the caller in two conditions, when:
return statement is executed
it reaches the method ending closing
brace.
public class ExampleVoid{
Public static void main(String[]args){
methodRankPoints(255.7);
}
public static void methodRankPoints(double points){
If (points>=202.5){
System.out.println(“Rank:A1”);
}
Else if (points>=122.4) {System.out.println(“Rank:A2”);
}
else{
System.out.println(“Rank:A3”);
}
}
}
OUTPUT: RankA1
PASSING PARAMETERS BY VALUE
While working under calling process,
arguments is to be passed.
These should be in the same order as
their respective parameters in the
method specification.
Parameters can be passed by value
or by reference.
Passing Parameters by Value means
calling a method with a parameter.
Through this, the argument value is
passed to the parameter.
public class swappingExample {
public static void main (String[] argos) {
int a = 30;
int b = 45;
System.out.println (“Before swapping, a = “+ a
+” and b= “+b);
swapFunction(a,b);
System.out.println(“\n**Now, Before and After
swapping values will be same here* * : ”);
System,out.println (“After swapping, a = “+a+”
and is “+b);
}
Public static void swapFunction (int x, int y) {
System.out.println(“Before swapping(Inside),
a= “+ a +” b = “+ b);
Int z= x; x=y; y=z;
System,out.println (“After swapping Inside), a
= “+a+”b = “+b);
Write a Java method to display the
middle character of a string.
If the length of the string is odd there
will be open middle character.
pf3
pf4
pf5

Partial preview of the text

Download INTERMIDIATE PROGRAMMING LESSON REVIEWER and more Study notes Biology in PDF only on Docsity!

ADPROG

JAVA-METHODS

➢ JAVA METHOD

  • is a collection of statements that are grouped together to perform an operation.
  • When you call the: “System.out.println( ) method, for example, the system actually executes several statements in order to display a message on the console. Create own methods or without return values ● Invoke a method with or without parameters, and apply method abstraction in the program design. ➢ SYNTAX public static int methodName(int a, int b) { //body } ● public static - modifier ● i nt - return type ● methodName- name of the method ● a,b- formal parameters ● int a, int b - list of parameters ➢ METHOD CALLING
  • it should be called.
  • there are two ways in which a method is called: ● return a value or ● returning nothing (no return value)
  • the process of “method calling” is simple.
  • when a program invokes a method, the program control gets transferred to the “called method”
  • this “called method” the returns control to the caller in two conditions, when: ● return statement is executed ● it reaches the method ending closing brace. public class ExampleVoid{ Public static void main(String[]args){ methodRankPoints(255.7); } public static void methodRankPoints(double points){ If (points>=202.5){ System.out.println(“Rank:A1”); } Else if (points>=122.4) {System.out.println(“Rank:A2”); } else{ System.out.println(“Rank:A3”); } } } OUTPUT: RankA

➢ PASSING PARAMETERS BY VALUE

● While working under calling process, arguments is to be passed. ● These should be in the same order as their respective parameters in the method specification. ● Parameters can be passed by value or by reference. ● Passing Parameters by Value means calling a method with a parameter. ● Through this, the argument value is passed to the parameter. public class swappingExample { public static void main (String[] argos) { int a = 30; int b = 45; System.out.println (“Before swapping, a = “+ a +” and b= “+b); swapFunction(a,b); System.out.println(“\n*Now, Before and After swapping values will be same here * : ”); System,out.println (“After swapping, a = “+a+” and is “+b); } Public static void swapFunction (int x, int y) { System.out.println(“Before swapping(Inside), a= “+ a +” b = “+ b); Int z= x; x=y; y=z; System,out.println (“After swapping Inside), a = “+a+”b = “+b); ● Write a Java method to display the middle character of a string. ● If the length of the string is odd there will be open middle character.

JAVA-OOP

  • stands for “OBJECT-ORIENTED PROGRAMMING”
  • procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.WHAT IS OOP?
  • Object-oriented programming has several advantages over procedural programming: ● OOP is faster and easier to execute ● OOP provide a clear structure for the programs ● OOP helps to keep the Java code D RY “DON’T REPEAT YOURSELF” , and makes the code easier to maintain, modify and debug. ● OOP makes it possible to create full reusable applications with less code and shorter development time.DRY PRINCIPLE
  • is about reducing the repetition of code.
  • you should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating. ➢ **CLASSES & OBJECTS
  • classes and objects** are the two main aspects of object-oriented programming.
  • Look at the following illustration to see the dierence between class and objects: CLASS OBJECTS Fruit Apple Banana Mango CLASS OBJECTS Car Audi Toyota Honda
  • a class is a template for objects, and an object is an instance of a class.
  • when the individua l objects are created, they inherit all the variables and methods from the class.CLASSES & OBJECTS
  • Java is an object-oriented programming language.
  • everything in Java is associated with classes and objects, along with its attributes and methods. Example: ● Car is an object - the car “attributes” , such as weight, and color, and “methods” , such as drive and brake. - class is like an object constructor, or a “blueprint” for creating objects.CREATE A CLASS - to create a class, use the keyword class: Main.java Create a class named “Main” with a variable x: public class Main { int x = 5; } - class should always start with an uppercase first letter, and that the name of the java file should match the class name.CREATE AN OBJECT - an object is created from a class. - we already created the class named “Main”, so now we can use this to create objects. - to create an object of “Main”, specify the class name, followed by the object name, and use the keyword new: EXAMPLE: - create an object called “myObj” and print the value of x: public class Main { int x = 5; public static void main(String [] args) { Main myObj = new Main(); System.out.println( myObj.x ); } } EXAMPLE: create two objects of Main public class Main { int x = 5; public static void main(String [] args){ Main myObj1 = new Main();//Object 1 Main myObj2 = new Main();//Object 2 System.out.println(myObj2.x); } } ➢ JAVA CLASS ATTRIBUTES - in the previous chapter, we used the term “variable” for x in the example (as shown below). - it is actually an attribute of the class. - class attributes are variables within a class. EXAMPLE: create a class called “Main” with two attributes: x and y: public class Main { int x = 5;

METHODS IN JAVA OOP

➢ JAVA CLASS METHODS

  • you learned from the Java Method chapter that methods are declared within a class, and that they are used to perform certain actions: EXAMPLE: create a method named myMethod() in Main: public class Main { Static void myMethod() { System.out.println (“Hello World!”); } } myMethod() prints a text (the action) , when it is called. To call a method, write the method's name followed by two parentheses () and a semicolon; ➢ STATIC VS. PUBLIC
  • you will often see Java programs that have either static or public attributes and methods.
  • In the example above, we created a static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be accessed by objects. ● An example to demonstrate the dierences between “static” and “public methods” public class Main { //Static method static void myStaticMethod() { System.out.println (“Static methods can be called without creating objects”) //Public method public void myPublicMethod() { System.out.println(“Public methods must be called by creating objects”); } //Main method public static void main(String[]args) { myStaticMethod(); //Call the static method// // myPublicMethod(); This would compile an error Main myObj = new Main(); // Create an object of Main myObj.myPublicmethod(); //Call the public method on the object } }

➢ ACCESS METHODS WITH AN OBJECT

➢ USING MULTIPLE CLASSES

  • remember that the name of the java file should match the class name.
  • In this example, we have created two files in the same directory: ● Main.java ● Second.java public class Main { Public void fullThrottle() { System.out.println (“The car is going as fast as it can!”); } public void speed (int maxSpeed) { System.out.println (“Max speed is: “ + maxSpeed); } class Second { public static void main (String[] args) { Main myCar = new Main (); //Create a myCar object my Car.fullThrottle();// Call the fullThrottle() method myCar.speed(200);//Call the speed()method } }

ACCESS MODIFIERS IN JAVA

➢ TWO TYPES OF MODIFIERS IN JAVA

● Access modifiers ● Non-access modifiers ➢ ACCESS MODIFIERS

  • specifies the accessibility or scope of a field, method, constructor, or class.
  • we change the access level of the field, constructors, methods, and class by applying the access modifier on it.FOUR TYPES OF JAVA ACCESS MODIFIERS ● Private
  • The access level of the private modifier is only within the class.
  • it cannot be accessed from outside the class. ● Default
  • access level of a default modifier is only within the package.
  • it cannot be accessed from outside the package.
  • if you do not specify any access level, it will be the default. ● Protected
  • access level of a protected modifier is within the package and outside the package through child class.
  • if you do not make the child class, it cannot be accessed from outside the package. ● Public
  • access level of a public modifier is everywhere.
  • it can be accessed from within the class, outside the class, within the package and outside the package.UNDERSTANDING JAVA ACCESS MODIFIERS
  • let’s understand the access modifiers in Java by a simple table. ACCESS MODIFIER WITHIN CLASS WITHIN PACKAGE OUTSIDE PACKAGE SUBCLASS ONLY OUTSIDE PACKAGE PRIVATE (^) Y N N N DEFAULT (^) Y Y N N PROTECTED (^) Y Y Y N PUBLIC (^) Y Y Y Y

➢ PRIVATE

  • access modifier is accessible only within the class.DEFAULT
  • if you don’t use any modifier, it is treated as default by default.
  • accessible only within the package.
  • it cannot be accessed from outside the package.
  • it provides more accessibility than private.
  • but more restrictive than protected and public.PROTECTED
  • accessible within the package and outside the package but through inheritance only.
  • can be applied on the data member, method and constructor.
  • can’t be applied to the class. ➢ PUBLIC
  • accessible everywhere
  • it has the widest scope among all other modifiers.

JAVA CONSTRUCTOR

➢ JAVA CONSTRUCTOR

  • special method that is used to initialize objects.
  • constructor is called when an object of the class is created.
  • it can be used to set initial values for object attributes.

JAVA ENCAPSULATION

➢ ENCAPSULATION

  • to make sure that “sensitive” data is hidden from users. TO ACHIEVE THIS you must: ● Declare class variables/attributes as private ● Provide public get and set methods to access and update the value of a private variable.