Beruflich Dokumente
Kultur Dokumente
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
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();
}
}
}
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("sorting...");
if (a > b) {
t = b;
b = a;
a = t;
}
if (a > c) {
t = c;
c = a;
a = t;
}
xxxx-xxxx-xxxx // NAME//
if (b > c) {
t = c;
c = b;
b = t;
}
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 );
}
}
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!");
xxxx-xxxx-xxxx // NAME//
if(t<=60)
System.out.println("\n\t Can I help you?");
}
}
xxxx-xxxx-xxxx // NAME//
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
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();
}
}
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//
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//
(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;
}
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 );
}
}
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.