When lists of numbers are output they are sent to the Java Console Output window.
For simple input and output TextFields are used with ActionListeners to wait for
user to enter values. You can enter new values whenever you like without rerunning program.
product | product2 | series |
series_test | tasks | Local Variables |
import java.awt.*; public class product { public static void main(String args[]) { double m = 2; double a = 4; double f = m*a; System.out.println(String.valueOf(f)); //print to Java Console } }
Program product2
Program series_test
Program series
import java.awt.*;
import java.awt.event.*;
public class product2 extends Frame implements ActionListener {
TextField in1,in2,out1;
public static void main(String args[]) {
product2 pr2 = new product2();
}
public product2() {
setSize(500,200);
init();
setVisible(true);
}
public void init() {
setLayout(new FlowLayout()); // use a Layout manager
in1 = new TextField("Enter mass here");
in2 = new TextField("Enter acceleration here");
out1 = new TextField("Force given here");
in1.addActionListener(this); // call actionPerformed when
in2.addActionListener(this); // numbers entered in these TextFields
add(in1);
add(in2);
add(out1);
}
public void actionPerformed(ActionEvent e) {
Double m = new Double(in1.getText().trim()); //use Double class to convert string to number
Double a = new Double(in2.getText().trim());
double f = m.doubleValue()*a.doubleValue();
out1.setText(String.valueOf(f)); // output to TextField
}
}
import java.awt.*;
public class series {
public static void main(String args[]) {
/* add the first 100 terms of a simple series */
double sum = 0;
for(int n = 1; n <= 100; n++) {
sum = sum + 1.0/(n*n);
System.out.println(n + " " + sum);
}
}
}
import java.awt.*;
public class series_test {
public static void main(String args[]) {
/* illustrate use of while loop */
double sum = 0; // initialize variables
int n = 0;
double relative_change = 1; // choose large value
while(relative_change > 0.0001) {
n++; // shorthand for n = n +1
double newterm = 1.0/(n*n);
sum += newterm; // shorthand for sum = sum + newterm
relative_change = newterm/sum;
System.out.println(n + " " + relative_change + " " + sum);
}
}
}