public class ReturnArrayFromMethod {
public static double[] doubleArray(double []array){
double newmarks[] = new double[array.length];
for (int i=0;i<array.length;i++)
newmarks[i] = array[i] * 2;
return newmarks;
} //end printArray
public static void printArray(double []array){
for (int i=0;i<array.length;i++)
System.out.println(array[i]);
} //end printArray
public static void main(String[] args) {
double marks[] = {45.5,46,49,13};
double marks2[] = doubleArray(marks);
printArray(marks);
System.out.println("========");
printArray(marks2);
}//end main
} //end class
Tuesday, December 05, 2006
Return Array From Method
PassArray To Method , Pass By Value
public class PassArrayToMethodExample {
public static void doubleInt(int num){
num= num*2;
System.out.println(num);
}
public static void doubleArray(int []array){
for (int i=0;i<array.length;i++)
array[i] = array[i] * 2;
} //end printArray
public static void printArray(int []array){
for (int i=0;i<array.length;i++)
System.out.println(array[i]);
} //end printArray
public static void main(String[] args) {
int num = 5;
doubleInt(num);
System.out.println(num);
int a[] = {5,10,20,30,40};
int b[] = {15,2,330,40};
int c[] = {40};
int d[] = {5,50,320,40,40,100,200,300,400};
doubleArray(a);
printArray(a);
} //end main
}//end class
Friday, December 01, 2006
Create JButton Array Example
import javax.swing.*;
import java.awt.*; // layout
public class JButtonArrayExample extends JFrame{
private JButton btn[] = new JButton[20];
public JButtonArrayExample(){
for (int i=0; i <btn.length;i++){
btn[i] = new JButton("Button "+ i);
getContentPane().add(btn[i]);
}
getContentPane().setLayout(new FlowLayout());
}
public static void main(String[] args) {
JButtonArrayExample e = new JButtonArrayExample();
e.setSize(300,300);
e.setVisible(true);
} //end main
}//end class
Practical 10 Question 1
import javax.swing.*;
class p10q1 {
public static void main(String[] args) {
int[] intArray = {11,0,10,2,2};
int highest = 0;
String output = "";
for (int i=0; i<intArray.length;i++){
if (intArray[i]>highest)
highest = intArray[i];//11
if (i!= intArray.length-1) //if it is not the last element
output += intArray[i] + ",";
else
output += intArray[i]; //don't put comma
} //end for
// TODO: Use JOptionPane to show results : 1 line
// Hint: use output + highest
JOptionPane.showMessageDialog(null,
"Elements: " + output+
"\nHighest: " + highest);
}//end main
}//end class
3 Different Ways of Declaring, Creating and Initialising an Array
public class ArrayExample2 {
public static void main(String[] args) {
// Step 1: Declare
// Step 2: Create the array
// Step 3: Initialise
// Step 1
double temp0[];
// Steps1 +2
double temp1[] = new double[7];
// Step 3: initialising
temp1[0] = 37.5;
temp1[1] = 38.5;
// 3-in-1 method
// Declare,
double temp2[] = {37.5, 38.5, 39.5, 40.5,41.5};
System.out.println(temp2[2]);
// Retrieve the length of an array
System.out.println(temp2.length);
for (int i=0;i<temp2.length;i++){
System.out.println(temp2[i]);
}//end for
} //end main
} //end class
public static void main(String[] args) {
// Step 1: Declare
// Step 2: Create the array
// Step 3: Initialise
// Step 1
double temp0[];
// Steps1 +2
double temp1[] = new double[7];
// Step 3: initialising
temp1[0] = 37.5;
temp1[1] = 38.5;
// 3-in-1 method
// Declare,
double temp2[] = {37.5, 38.5, 39.5, 40.5,41.5};
System.out.println(temp2[2]);
// Retrieve the length of an array
System.out.println(temp2.length);
for (int i=0;i<temp2.length;i++){
System.out.println(temp2[i]);
}//end for
} //end main
} //end class
Calc. Average temperature example
public class CompleteTemperatureExample {
public static void main(String[] args) {
double []temp1 = new double[7];
double temp2[] = {39.0,39.5, 26, 28};
double total=0;
for (int i=0;i<temp2.length;i++){
total+=temp2[i];
} //end for
System.out.println("Average is" + total/temp2.length);
} //end main
} //end class
public static void main(String[] args) {
double []temp1 = new double[7];
double temp2[] = {39.0,39.5, 26, 28};
double total=0;
for (int i=0;i<temp2.length;i++){
total+=temp2[i];
} //end for
System.out.println("Average is" + total/temp2.length);
} //end main
} //end class
Demo Default Values of Primitive + Object
public static void main(String args[]) {
double []temperatures;
int numstudents[] = new int[5];
boolean hungry[] = new boolean[10];
char []grades = new char[3];
String names[] = new String[8];
// Retrieve second value
System.out.println("String:" + names[1]);
// Retrieve second value
System.out.println("char:" + grades[1]);
// Retrieve second value
System.out.println("boolean:" + hungry[1]);
// Retrieve second value
System.out.println("int:" + numstudents[1]);
// Allocate space to store 7 double values
temperatures = new double[7];
// Retrieve second value
System.out.println(temperatures[1]);
} //end main
Subscribe to:
Posts (Atom)