Você está na página 1de 15

xxxx-xxxx-xxxx // NAME//

Table of Contents
Table of Contents ............................................................................................................................ 1
Ques One. ........................................................................................................................................ 2
Ques Two. ........................................................................................................................................ 3
Ques Three. ..................................................................................................................................... 5
Ques Four. ....................................................................................................................................... 6
(a)................................................................................................................................................. 6
(b) ................................................................................................................................................ 9
Ques Five. ...................................................................................................................................... 10
(a)............................................................................................................................................... 10
(b) .............................................................................................................................................. 11
1. Encapsulation .................................................................................................................... 11
2. Inheritance ....................................................................................................................... 11
3. Polymorphism.................................................................................................................... 12
(c) ............................................................................................................................................... 13
1.Declaration ......................................................................................................................... 13
2. Initialization ....................................................................................................................... 13
References ..................................................................................................................................... 15

Introduction to Java | CPG101 1

xxxx-xxxx-xxxx // NAME//

QuesOne.
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print( i * j );
System.out.print("\t");
}
System.out.println();
}
}
}

Introduction to Java | CPG101 2

xxxx-xxxx-xxxx // NAME//

QuesTwo.
public class SortThreeNumbers {
public static void main(String[] args) {
int a = 3;
int b = 1;
int c = 2;
int t;

System.out.println("a = " + a);


System.out.println("b = " + b);
System.out.println("c = " + c);

System.out.println("sorting...");

if (a > b) {
t = b;
b = a;
a = t;
}
if (a > c) {
t = c;
c = a;
a = t;
}

Introduction to Java | CPG101 3

xxxx-xxxx-xxxx // NAME//

if (b > c) {
t = c;
c = b;
b = t;
}

System.out.println("a = " + a);


System.out.println("b = " + b);
System.out.println("c = " + c);
}

Introduction to Java | CPG101 4

xxxx-xxxx-xxxx // NAME//

QuesThree.
importjavax.swing.JOptionPane;
import java.io.*;
public class KilomConvertion{
public static void main(String[]args)throws Exception{
String kilo=JOptionPane.showInputDialog(null, "Enter Value (Kilometers):", "Input",
JOptionPane.QUESTION_MESSAGE );
Double km=Double.parseDouble(kilo);
Double mil=km*0.621;
JOptionPane.showMessageDialog(null,+km+" Kilometers is equal to "+mil+" Miles", "Message",
JOptionPane.PLAIN_MESSAGE );
}
}

Introduction to Java | CPG101 5

xxxx-xxxx-xxxx // NAME//

QuesFour.
(a)
import java.io.*;
public class TermScore
{
public static void main(String[]args)throws Exception
{
BufferedReader input= new BufferedReader (new
InputStreamReader(System.in));
doublep,q,r,s,t;
System.out.print("ENTER MID TERM SCORE:");
p=Double.parseDouble(input.readLine());
System.out.print("ENTER FINAL SCORE:");
q=Double.parseDouble(input.readLine());

r=(p/100)*40;
s=(q/100)*60;
t=r+s;
if(t>90)
System.out.println("\n\t Outstanding result!");

if(t>85 && t<90)


System.out.println("\n\t Nicework!");

if(t>60 && t<85)


Introduction to Java | CPG101 6

xxxx-xxxx-xxxx // NAME//

System.out.println("\n\t You can do better than that!");

if(t<=60)
System.out.println("\n\t Can I help you?");
}
}

Introduction to Java | CPG101 7

xxxx-xxxx-xxxx // NAME//

Introduction to Java | CPG101 8

xxxx-xxxx-xxxx // NAME//

(b)

Start

Get
TermScore
No

Yes
Mark
>90

Outstanding
result

No

Yes
Nice work

Mark
s >85
No

Yes
Mark
s<60

You can do
better

Can I help
you?

Stop

Introduction to Java | CPG101 9

xxxx-xxxx-xxxx // NAME//

Ques Five.
(a)
Import java.io.*;
public class Mynum{
public static void main (string args [] )
{
for(int counter = 1; counter<=100; counter ++)
1+= 2;
System.out.printf(%i,counter);
System.out.println();
}
}

Introduction to Java | CPG101 10

xxxx-xxxx-xxxx // NAME//

(b)
1. Encapsulation: This is one of the fundamental principles of Object-Oriented
programming. It is the phenomenon whereby attributes and methods are bind together
from user or client in order to protect it from misuse or interference. Classes use java
access modifiers to achieve this goal.Henceforth, a method that is intended to be access
by the user is going to be hiding from them, either by declaring the variables or methods
with private.A typical example is when a programmer need to present class in two
ways.The first contract can be access by the user of the same.While the second contract is
accessible by subclasses of different class,in some cases the programmer may tend to
make a specific method or class private so that the accessor and mutator method to be
public.If they are intend to make the class accessible for the subclass of same
package header.But in another vein, programmer my wish to make variable or methods
protected if he want that method to be accessed by subclasses of different packages.
Import java.io.*;
classOuterClass
{
private var $innerobject;
function increment()
{
return $this->innerobject->increment();
}
}
2. Inheritance: Is the process use in by software developers, which involves creating a
single class that absorbs an existing class members and embellishing them with new or
modified capabilities or features.However, inheritance helps and save programmers
time and effort.As whenever they intend to use a class they dont need to declare it,
they only designate that the new class should inherit the members of the existing
class,that means the existing class serve as the super class to the new once (subclass).
class A {
int x;
int y;
Introduction to Java | CPG101 11

xxxx-xxxx-xxxx // NAME//

int get(int p, int q){


x=p; y=q; return(0);
}
void Show(){
System.out.println(x);
}
}

class B extends A{
public static void main(String args[]){
A a = new A();
a.get(5,6);
a.Show();
}
void display(){
System.out.println("B");
}
}

3. Polymorphism: Is the combination of two Greek words poly, which means many,
andmorphism, which means forms.Henceforth, Polymorphism is a phenomenon
where by an object with the same name having different implementation or appearance of
features.A typical example of under this is the cellular Telephone services such as IDN
and ISDN. Digital cellular services uses more advance Technologies both has small
geographic service region than does.The dual-model analog to digital cellular phone is
an example of established analog which provides a single application but employs two
different technologies.
Import java.io.*;
class TradingSystem{
Introduction to Java | CPG101 12

xxxx-xxxx-xxxx // NAME//

public String getDescription(){


return "electronic trading system";
}
}
class DirectMarketAccessSystem extends TradingSystem{
public String getDescription(){
return "direct market access system";
}
}
class CommodityTradingSystem extends TradingSystem{
public String getDescription(){
return "Futures trading system";
}
}

(c)
1.Declaration
class Puppy{
public Puppy(String name){
System.out.println("Passed Name is :" + name );
}
public static void main(String []args){
Puppy myPuppy = new Puppy( "tommy" );
}
}

2. Initialization
class Puppy{
intpuppyAge;
public Puppy(String name){
System.out.println("Passed Name is :" + name );
}
publicsetAge(int age ){
puppyAge = age;
}

Introduction to Java | CPG101 13

xxxx-xxxx-xxxx // NAME//

publicgetAge( ){
System.out.println("Puppy's age is :" + puppyAge );
returnpuppyAge;
}
public static void main(String []args){
Puppy myPuppy = new Puppy( "tommy" );
myPuppy.setAge( 2 );
myPuppy.getAge( );
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}

Introduction to Java | CPG101 14

xxxx-xxxx-xxxx // NAME//

References
3.5. Data Hiding and Encapsulation [Online] / auth. O'Reilly & Associates // JAVA in a
NUTSHELL. - 2011. - docstore.mik.ua/orelly/java-ent/jnut/ch03_05.htm.
AP Computer Science Curriculum Module: An Introduction to Polymorphism in Java [Book] /
auth. Umbarger Dan. - Dallas, Texas : Collage Board, 2008.
Convert Meters To Miles [Online] / auth. Rose India Technologies PVT LMT. // Rose India. www.roseindia.net/java/java-conversion/convert-meters-to-miles.shtml.
Introduction to Java (CPG101) [Book] / auth. Holding Informatics. - Singapour : Informatics
Institute, 2009.
Java Objects and Classes [Online] / auth. tutorialspoint.com // Tutorials Point. - 2012. http://www.tutorialspoint.com/java/java_object_classes.htm.
What is encapsulation in Java? [Online] / auth. SearchSOA.com // SearchSOA. searchsoa.techtarget.com/answer/What-is-encapsulation-in-Java.

Introduction to Java | CPG101 15

Você também pode gostar