Professional Documents
Culture Documents
Course Instructor: Nazish Basir Page: 1/13 Institute of Information and Communication Technology, University of Sindh
Course Instructor: Nazish Basir Page: 1/13 Institute of Information and Communication Technology, University of Sindh
class Test
{
public static void sum(int[] arr) // getting sum of array values
{
int sum = 0;
for (int i = 0; i < arr.length; i++)
sum+=arr[i];
System.out.println("sum of array values : " + sum);
}
A method can have variable length parameters with other parameters too, but one should ensure that there exists
only one varargs parameter that should be written last in the parameter list of the method declaration.
int nums(int a, float b, double … c)
In this case, the first two arguments are matched with the first two parameters and the remaining arguments
belong to c.
Vararg Methods can also be overloaded but overloading may lead to ambiguity.
Prior to JDK 5, variable length arguments could be handled into two ways : One was using overloading,
other was using array argument.
There can be only one variable argument in a method.
Variable argument (varargs) must be the last argument.
class Test1 {
// A method that takes variable number of integer arguments.
static void fun(int ...a) {
System.out.println("Number of arguments: " + a.length);
// using for each loop to display contents of a
for (int i: a)
System.out.print(i + " ");
System.out.println();
}
class VarArgs3 {
static void vaTest(int ... v) {
System.out.print("vaTest(int ...): " + "Number of args: " + v.length +
" Contents: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
static void vaTest(boolean ... v) {
System.out.print("vaTest(boolean ...) " + "Number of args: " +
v.length + " Contents: ");
for(boolean x : v)
System.out.print(x + " ");
System.out.println();
}
static void vaTest(String msg, int ... v) {
System.out.print("vaTest(String, int ...): " + msg + v.length +
" Contents: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
} public static void main(String args[])
{
vaTest(1, 2, 3);
vaTest("Testing: ", 10, 20);
vaTest(true, false, false);
}
}
Java provides the JOptionPane class for creating dialog boxes—those familiar pop-up windows that prompt the
user to enter a value or notify the user of an error. The JOptionPane class is in the javax.swing package.
A wrapper class “wraps” the value of a primitive type, such as double or int, into an object. These wrapper classes
define an instance variable of that primitive data type, and also provide useful constants and methods for
converting between the objects and the primitive data types.
import javax.swing.JOptionPane;
class Test {
public static void main(String a[]){
JOptionPane.showMessageDialog(null, "Hello");
String name;
name = JOptionPane.showInputDialog("What is your name?");
JOptionPane.showMessageDialog(null, "Hello "+ name);
System.out.println();
}
}
PROGRAM 5: Using parseInt() and the JOptionPane class for creating dialog boxes
import javax.swing.JOptionPane;
class Test {
public static void main(String arg[]){
int a,b;
a = Integer.parseInt(JOptionPane.showInputDialog("Enter 1st Value:"));
b = Integer.parseInt(JOptionPane.showInputDialog("Enter 2nd Value:"));
JOptionPane.showMessageDialog(null, a+" + " +b+ " = "+ (a+b));
System.out.println();
}
}
PROGRAM 6:
import javax.swing.JOptionPane;
class MainMenu {
int option;
public void mainMenu() {
do {
option = Integer.parseInt(JOptionPane.showInputDialog("SELECT FROM
THIS MENU" + "\n1.Calculator\n2.Convertor\n3.Marksheet\n4.Exit"
+ "\nEnter Your Choice "));
switch(option) {
case 1:
Calculator cl = new Calculator();
cl.menu();
break;
case 2:
JOptionPane.showMessageDialog(null,"Convertor");
break;
case 3:
JOptionPane.showMessageDialog(null,"Marks Sheet");
break;
case 4:
JOptionPane.showMessageDialog(null,"Thank You for Using");
break;
default:
JOptionPane.showMessageDialog(null,"Invalid Option");
}
}
while(option !=4);
}
}
class Calculator {
int n1,n2, option;
char ch;
do {
option=Integer.parseInt(JOptionPane.showInputDialog("Calculator\n"
+ "\n1Add\n2.Sub\n3.Multi\n4.Division\n5.Back"));
if(option == 1) {
add();
}
else if(option == 2) {
sub();
}
else if(option == 3) {
mul();
}
else if(option == 4) {
div();
}
else if (option == 5) {
JOptionPane.showMessageDialog(null, "Thank you for using Calculator");
}
}while(option != 5);
}
class MultiProgramClient {
public static void main(String[] args) {
MainMenu ob = new MainMenu();
ob.mainMenu();
}
}
EXERCISE:
Run program number 3 and see the results. Now create two more classes Converter and MarkSheet. (See
Calculator class for the design of both classes)
In Converter, use any 4 options for conversion of your choice. For Example Kilogram to Pounds, Hours to
Minuets or Degree Celsius to Degree Fahrenheit, etc. The program should show a menu for choice. When
user selects the choice the input box should take value of unit from user and convert it accordingly. The
program should ask if you want to convert another value.
In MarkSheet, take five subject of your choice and show in menu then when a user select a choice the
program should take input of marks from user for each subject. Finally shows the total, percentage and
grade of that student.
INHERITANCE IN JAVA
Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain
an application.
This also provides an opportunity to reuse the code functionality and fast implementation time. When creating
a class, instead of writing completely new data members and member methods, the programmer can designate
that the new class should inherit the members of an existing class.
The idea of inheritance implements the “is a” relationship. For example, mammal IS-A animal, dog IS-A
mammal hence dog IS-A animal as well and so on.
Inheritance lets us organize related classes into ordered levels of functionality, called hierarchies. The
advantage is that we write the common code only once and reuse it in multiple classes.
A subclass inherits methods and fields of its superclass. A subclass can have only one direct superclass, but
many subclasses can inherit from a common superclass.
Inheritance implements the “is a” relationship between classes. Any object of a subclass is also an object of the
superclass.
All classes inherit from the Object class.
To specify that a subclass inherits from a superclass, the subclass uses the extends keyword in the class
definition, as in the following syntax:
AccessModifier class CassName extends SuperClassName
A subclass does not inherit constructors or private members of the superclass. However, the superclass
constructors are still available to be called from the subclass, and the private fields of the superclass are
implemented as fields of the subclass.
To access private fields of the superclass, the subclass needs to use the methods provided by the superclass.
To call the constructor of the superclass, the subclass constructor uses the following syntax:
super ( argument list );
If used, this statement must be the first statement in the subclass constructor.
A subclass can override an inherited method by providing a new version of the method. The new method’s API
must be identical to the inherited method. To call the inherited version of the method, the subclass uses the
super object reference using the following syntax:
super.methodName ( argument list );
Any field declared using the protected access modifier is inherited by the subclass. As such, the subclass can
directly access the field without calling its method.
In Multilevel Inheritance, a subclass will be inheriting a superclass and as well as the subclass also act as the
superclass to other class. In below image, the class A serves as a superclass for the subclass B, which in turn
serves as a superclass for the subclass C. In Java, a class cannot directly access the grandparent’s members.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
class DemoBoxWeight {
public static void main(String args[]) {
double vol;
vol = mybox1.volume();
vol = mybox2.volume();
class A {
int i;
}
class B extends A {
int i;
B(int a, int b) {
super.i = a;
i = b;
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
class A {
int i;
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
// Add weight.
class BoxWeight extends Box {
double weight; // weight of box
// default constructor
BoxWeight() {
super();
weight = -1;
}
// default constructor
Shipment() {
super();
cost = -1;
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 =
new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 =
new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is "
+ shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is "
+ shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}