uNreQuiteD

January 23, 2007

FOR loop: POWER

Filed under: basic java — anai lem @ 1:39 am

/*
Create a program that will compute the power of a number given the base
and exponent. Do this program using a for loop.
programmed by: eugene
spec fr: JEDI – PROGRAMMING LANGUAGE 1
*/

import javax.swing.*;
class powers
{
public static void main(String args[])
{
int base = Integer.parseInt(JOptionPane.showInputDialog(“ENTER BASE:”));
int power = Integer.parseInt(JOptionPane.showInputDialog(“ENTER EXPONENT:”));
int result=1;
for(int x=1; x

Polymorphism

Filed under: object oriented progg — anai lem @ 12:31 am

/*SAMPLE POLYMORPHISM CLASS
compiled and edited by eugene.
reference:JEDI – Introduction to Programming 1 / pp 184-186
*/

class person
{
String name;
public String getName()
{
System.out.println(“person name:” + name);
return name;
}
}

class student extends person
{
public String getName()
{
System.out.println(“student name:” + name);
return name;
}
}

class Employee extends person
{
public String getName()
{
System.out.println(“employee name:” + name);
return name;
}
public static void main(String args[])
{
person ref;
student st = new student();
Employee em = new Employee();

ref = st;
ref.name = “iana”;
String temp = ref.getName();
System.out.println(temp+”\n”);

ref = em;
ref.name = “mel”;
temp = ref.getName();
System.out.println(temp+”\n”);

ref = new person();
ref.name = “iana mel”;
temp = ref.getName();
System.out.println(temp);
}
}

output

student name:iana
iana
employee name:mel
mel

person name:iana mel
iana mel

Blog at WordPress.com.