Basic Object Oriented Programming concept, Study Guides, Projects, Research of Object Oriented Programming

All Basic program for new student of Java Language

Typology: Study Guides, Projects, Research

2018/2019

Uploaded on 07/06/2019

mukhtar-khawaja
mukhtar-khawaja 🇵🇰

2 documents

1 / 60

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assignment:
Object Oriented Programing
(OOP)
Name:
Mukhtar Ahmed
Teacher Name:
Mam Rawish Butt
Question # 1
Use of “this” keyword
CODE:
package thisword;
import java.util.Scanner;
class Account
{
String name;
void setName(String name)
{
this.name=name;
}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c

Partial preview of the text

Download Basic Object Oriented Programming concept and more Study Guides, Projects, Research Object Oriented Programming in PDF only on Docsity!

Assignment:

Object Oriented Programing

(OOP)

Name:

Mukhtar Ahmed

Teacher Name:

Mam Rawish Butt

Question # 1

Use of “this” keyword

CODE:

package thisword; import java.util.Scanner; class Account { String name; void setName(String name) { this.name=name; }

String getName() { return name; } } public class ThisWord { public static void main(String[] args) { Scanner input=new Scanner(System.in); Account obj=new Account(); System.out.println("Please Enter the name"); String name=input.nextLine(); obj.setName(name); String disp=obj.getName(); System.out.printf("Name in object obj is %n%s%n",disp); } }

OUTPUT:

System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; } } public class Labb { public static void main(String[] args) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume();

System.out.println("Volume is " + vol); } }

OUTPUT:

Question # 3:

Constructor(Parameterized Constructor)

CODE:

package defaultconstractor; import java.util.Scanner; class First { int a=0; int b=0;

OUTPUT:

Question # 4:

Arrays (One-Dimensional Arrays)

CODE:

package labb; public class Labb { public static void main(String[] args) { int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

System.out.println("April has " + month_days[3] + " days."); } }

OUTPUT:

Question # 5:

Arrays(Multidimensional Arrays)

CODE:

package labb; public class Labb { public static void main(String[] args) { int twoD[][]= new int[4][5];

Question # 6:

Java’s Selection Statements(Nested if else)

CODE:

package labb; public class Labb { public static void main(String[] args) { int month = 4; // April String season; String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else

if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); } }

OUTPUT:

break;

case 3:

System.out.println("i is three.");

break;

default:

System.out.println("i is greater than 3.");

OUTPUT:

Question # 8:

Iteration Statements (while Loop)

CODE:

package labb; public class Labb { public static void main(String[] args) { int n = 10; while(n > 0) { System.out.println("tick " + n); n--; } } }

OUTPUT:

System.out.println("tick " + n); n--; } while(n > 0); } }

OUTPUT:

Question # 10:

Iteration Statements (For Loop)

CODE:

package labb; public class Labb

public static void main(String[] args) { int n; for(n=10; n>0; n--) System.out.println("tick " + n); } }

OUTPUT:

Question # 11:

Iteration Statements (For each Statement/Loop)

CODE:

package enhanceforloop; public class EnhanceForLoop {

CODE:

package labb; class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a;

public class Labb { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); } }

OUTPUT: