COMPUTER PROGRAMMING I 2013 WEEK
4
STATIC
METHODS
EXERCISES
(EXERCISE problems are for your understanding the
topic. Will not return as homework)
In
order to understand the topics, write down exercise problems and see that they
are working properly, this will give you the backgroud to solve the homework
problems. You are required to return only the homework problem codes and
solutions. In exam, some of the example or homework problems will be asked as
questions, so be sure you understand all of them properly.
IF STATEMENT
import javax.swing.*; class W4E1 { public
static String action(String weather) { String s; if(weather.equals("clear")) {s="go for walking in
the park";} else if(weather.equals("rainy")) {s="study computer
programming I";} else if(weather.equals("cloudy")) {s="go to the movie
theater";} else {s="Weather condition is
not given";} return s; } public static void print(String s,String s1) {JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);} public static void main(String args[]) { String weather; weather=JOptionPane.showInputDialog("wheather
condition: clear/rainy/cloudy");
print(action(weather),"======If statement======"); } } |
class W4E1A { public static void main(String args[]) { String weather; weather=JOptionPane.showInputDialog("wheather
condition: clear/rainy/cloudy"); String s=W4E1.action(weather); String s1="======If
statement======"; W4E1.print(s,s1); } } |
import javax.swing.*; class
W4E2 { public static String action(int n) { String s=""; if(n==1) {s="go for walking in
the park";} else
if(n==2) {s="study computer
programming I";} else
if(n==3) {s="go to the movie
theater";} else {s="Weather condition
is not given";} return s; } public static void main(String args[]) { String weather; weather=JOptionPane.showInputDialog("wheather condition:\n 1:clear\n
2:rainy\n 3:cloudy"); W4E1.print(action(Integer.parseInt(weather)),"======If
statement======"); } } |
import javax.swing.*; class
W4E2A { public static void main(String args[]) { String weather;
weather=JOptionPane.showInputDialog("wheather
condition:\n 1:clear\n 2:rainy\n 3:cloudy"); String w=W4E2.action(Integer.parseInt(weather)); W4E1.print(w,"======If
statement======"); } } |
import javax.swing.*; public
class W4E3 { public
static double determinant(double a,double b,double c) {return
b*b-4.0*a*c; } public
static String root_type(double det) { boolean b1=det>0; boolean b2=det<0; boolean b3=det==0; String s=""; if(b1) {s+="determinant greater than 0
\nthe roots are real";} else if(b2) {s+="determinant smaller than 0\nthe
roots are complex";} else {s+="determinant are equal to 0\ntwo
equal real roots";} return s; } public
static void main(String arg[]) { //Calculate d=b*b-4.0*a*c; String sa=JOptionPane.showInputDialog("a="); String sb=JOptionPane.showInputDialog("b="); String sc=JOptionPane.showInputDialog("c="); double a=Double.parseDouble(sa); double b=Double.parseDouble(sb); double c=Double.parseDouble(sc); //Determinant double d=determinant(a,b,c); JOptionPane.showMessageDialog(null,root_type(d)); } } |
import javax.swing.*; public
class W4E3A { public
static double read(String s) {String
sa=JOptionPane.showInputDialog(s); double a=Double.parseDouble(sa); return a; } public
static void main(String arg[]) { //Calculate d=b*b-4.0*a*c; double a=read("a="); double b=read("b="); double c=read("c="); //Determinant double d=W4E3.determinant(a,b,c); String s="determinant =
"+d+"\n"+W4E3.root_type(d); String s1="root type of quadratic
equation"; W4E1.print(s,s1); } } |
Note
that this program checks out if the given string is an integer, if it is not an
integer it will give the error message
“the given input is not an integer"
import javax.swing.*; class
W4E4 { public static int toInt(String s) {String
s2="^-?[0-9]+(\\.[0-9]+)?$"; boolean b=s.matches(s2); int n=0; if(b) {n=Integer.parseInt(s);} else {W4E1.print("the given input is not an
integer 0 returned","ERROR MESSAGE");} return n; }
public static void main(String args[]) { String weather; weather=JOptionPane.showInputDialog("wheather
condition:\n 1:clear\n 2:rainy\n 3:cloudy");
W4E1.print(W4E2.action(toInt(weather)),"======If
statement======"); } } |
import javax.swing.*; class
W4E4A { public static String read(String s) {String sa=JOptionPane.showInputDialog(s); return sa; } public static void main(String args[]) { String weather; weather=read("wheather condition:\n 1:clear\n 2:rainy\n 3:cloudy"); W4E1.print(W4E2.action(W4E4.toInt(weather)),"======If
statement======"); } } |
Note
that this program checks out if the given string is a double number, if it is
not a double it will give the error message
“the given input is not a double number ". Do not wory about the
content of the comparison string for now, just use it as it is.
import javax.swing.*; class
W4E5 { public static double toDouble(String s) {String
s2="[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*"; boolean b=s.matches(s2); double n=0.0; if(b) {n=Double.parseDouble(s);} else {W4E1.print("the given input is not a
double 0 returned","ERROR MESSAGE");} return n; } public static void main(String args[]) { String s1; s1=JOptionPane.showInputDialog("enter
a double number : "); double x=toDouble(s1); String s="the given double
number is "+x; JOptionPane.showMessageDialog(null,s,"======If
statement in static method======",JOptionPane.PLAIN_MESSAGE); } } |
import javax.swing.*; class W4E5A { public static void main(String args[]) { String s1; s1=W4E4A.read("enter a double number : "); double x=W4E5.toDouble(s1); String s="the given double
number is "+x; JOptionPane.showMessageDialog(null,s,"======If
statement in static method======",JOptionPane.PLAIN_MESSAGE); } } |
WHILE STATEMENT
import javax.swing.*; class
W4E6 { public static String func(int x1,int x2) { int y; String s=""; int x=x1; while(x<=x2) {y=x*x+2*x+1; s+="x
= "+x+" y = "+y+"\n"; x++; } return s; } public static void main(String args[]) { int x1=0; int x2=20; JOptionPane.showMessageDialog(null,func(x1,x2),"======
function evaluation ======",JOptionPane.PLAIN_MESSAGE); } } |
import javax.swing.*; class
W4E6A { public static int read(String s) {String sa=JOptionPane.showInputDialog(s); int a=Integer.parseInt(sa); return a; } public static void main(String args[]) { int x1=read("x1="); int x2=read("x2="); String s=W4E6.func(x1,x2);
String s1="====== function evaluation ======"; W4E1.print(s,s1); } } |
import javax.swing.*; class
W4E7 { public static int sum(int x1,int x2) { int sum=0; int x=x1; while(x<x2) {sum+=x; x++; } return sum; } public static void main(String args[]) { //sum of the numbers from 1 to 20 int a1=1; int a2=20; String s=""; s+="
sum = "+sum(a1,a2)+"\n"; W4E1.print(s,"====== function
evaluation ======"); } } |
import javax.swing.*; class
W4E7A { public static void main(String args[]) { //sum of the numbers from 1 to 20 int a1=W4E6A.read("x1="); int a2=W4E6A.read("x2="); String s=""; s+="
sum = "+W4E7.sum(a1,a2)+"\n";
W4E1.print(s,"====== function
evaluation ======"); } } |
import javax.swing.*; class
W4E8 { public static int func(int x) {return x*x+2*x+1;} public static String plot(int x1,int x2) { int y=0; String s=""; String star="*"; String space=" "; String sum; int x=x1; while(x<=x2) { y=func(x);
// function to be plotted int i=0; sum=star; while(i<y) { sum =
space+sum; i++; } s+=sum+"\n"; x++; } return s; } public static void main(String args[]) { int x1=-10; int x2=10; W4E1.print(plot(x1,x2),"======
function plot ======"); } } |
import javax.swing.*; class
W4E8A { public static void main(String args[]) { int x1=W4E6A.read("x1="); int
x2=W4E6A.read("x2="); W4E1.print(W4E8.plot(x1,x2),"====== function plot ======"); } } |
In the next example a
more series will be calculated
note that
e=2.7182818…
import javax.swing.*; public
class W4E9 { public
static double exp(double x) { // number e
= 2.718... // exp(x) = 1 + x /1! + x2 / 2! + x3
/ 3! + x4 / 4! + .double power=1; double exponent=1; double factorial=1; int number=1; double power=1; while(number<=300) { factorial*=number; power*=x; exponent+=power/factorial; number++; } return exponent; } // main method begins execution of Java
application public static void main( String args[] ) { String sa=JOptionPane.showInputDialog("x=");
double x = Double.parseDouble(sa); String s="exp("+x+") =
"+exp(x); JOptionPane.showMessageDialog(null,s); } // end method
main } // end class |
public class W4E9A { // main method begins execution of Java
application public static void main( String args[] ) { double x = W4E3A.read("x=");
String s="exp("+x+") =
"+W4E9.exp(x)+"\nMath
library :"+Math.exp(x); W4E1.print(s,"exp"); } // end method
main } // end of class |
A series Formula for ln(x)
is given by
write program to calculate series
by using a while loop.. Note that equation can be further simplify if a second
conversion z=y*y is made
note that ln(2.718281)=1.0
import javax.swing.*; public
class W4E10 { public static double log(double x) {double y=(x-1.0)/(x+1.0); double power=2.0*y; double z=y*y; int n=0; double ln=0; while(n<=300) { ln+=1./(2.0*n+1.0)*power; power*=z; n++; } return ln; } // main method begins execution of Java
application public static void main( String args[] ) { // ln(x)
x>0 String sa=JOptionPane.showInputDialog("x
= "); double x = Double.parseDouble(sa); String s="ln("+x+") =
"+log(x); JOptionPane.showMessageDialog(null,s); } // end method
main } // end class BPIH4E1 |
import javax.swing.*; public
class W4E10A { // main method begins execution of Java
application public static void main( String args[] ) { double x =
W4E3A.read("x="); String s="log("+x+") =
"+W4E10.log(x)+"\nMath
library :"+Math.log(x); W4E1.print(s,"exp"); } // end method
main } // end of class |
(This
program combines two previous programs to calculate power function
import javax.swing.*; public
class W4E11 { public static double pow(double a,double
x) { return
W4E9.exp(x*W4E10.log(a)); } public static void main( String args[] ) { // a^x x>0
String sa=JOptionPane.showInputDialog("a
= "); double a = Double.parseDouble(sa);
String sx=JOptionPane.showInputDialog("x
= "); double x = Double.parseDouble(sx); String s=a+"^"+x+" =
"+pow(a,x); JOptionPane.showMessageDialog(null,s); } // end method
main } // end class |
import javax.swing.*; public class W4E11A { public static void main( String args[] ) { // a^x x>0
double a = W4E3A.read("a =
"); double x = W4E3A.read("x =
"); String s=a+"^"+x+" =
"+W4E11.pow(a,x)+"\nMath library :"+Math.pow(a,x); W4E1.print(s,"pow"); } // end method
main }
// end class |
FOR STATEMENT
import javax.swing.*; class
W4E12 { public
static String func(int x1,int x2) { int y; String s=""; for(int x=x1;x<x2;x++) {y=x*x+2*x+1; s+="x
= "+x+" y = "+y+"\n"; } return s; } public static void main(String args[]) { int x1=0; int x2=20; JOptionPane.showMessageDialog(null,func(x1,x2),"======
function evaluation ======",JOptionPane.PLAIN_MESSAGE); } } |
import javax.swing.*; class
W4E13 { public
static int sum(int x1,int x2) { int sum=0; for(int x=x1;x<x2;x++) {sum+=x;} return sum; } public static void main(String args[]) { //sum of the numbers from 1 to 20 int x1=1; int x2=20; String s=""; s+="
sum = "+sum(x1,x2)+"\n"; JOptionPane.showMessageDialog(null,s,"======
function evaluation ======",JOptionPane.PLAIN_MESSAGE); } } |
import javax.swing.*; class
W4E14 { public
static int func(int x) {return x*x+2*x+1;} public static String plot(int x1,int x2) { int y=0; String s=""; String star="*"; String space=" "; String sum; for(int x=x1;x<=x2;x++) { y=func(x);
// function to be plotted int i=0; sum=star; while(i<y) { sum =
space+sum; i++; } s+=sum+"\n"; } return s; } public static void main(String args[]) { int x1=-10; int x2=10; W4E1.print(plot(x1,x2),"====== function
plot ======"); } } |
In this program Math
library function Math.exp(x) is also used to compare
the results
import javax.swing.*; public
class W4E15 { public static double exp(double x) { // number e
= 2.718... // exp(x) = 1 + x /1! + x2 / 2! + x3
/ 3! + x4 / 4! + .double power=1; double exponent=1; double factorial=1; int number=1; double power=1; while(number<=300) { factorial*=number; power*=x; exponent+=power/factorial; number++; } return exponent; } // main method begins execution of Java
application public static void main( String args[] ) { String sa=JOptionPane.showInputDialog("x=");
double x = Double.parseDouble(sa); String s="exp("+x+") =
"+exp(x)+" Math library
value = "+Math.exp(x); JOptionPane.showMessageDialog(null,s); } // end method
main } // end class |
A series Formula for ln(x)
is given by
write program to calculate series
by using a for loop..
Note that equation can be further simplify if a second conversion z=y*y is made
In this program Math
library function Math.log(x) is also used to compare the results
import javax.swing.*; public
class W4E16 { public
static double log(double x) { // number e
= 2.718... // ln(e) = 1 double y=(x-1.0)/(x+1.0); double power=2.0*y; double z=y*y; int n=0; double ln=0; while(n<=300) { ln+=1./(2.0*n+1.0)*power; power*=z; n++; } return ln; } // main method begins execution of Java
application public
static void main( String args[] ) { String sa=JOptionPane.showInputDialog("x=");
double x = Double.parseDouble(sa); String s="exp("+x+") =
"+log(x)+" Math library
value = "+Math.log(x); JOptionPane.showMessageDialog(null,s); } // end method
main } // end class |
In this program Math
library function Math.pow(x) is also used to compare the results
import javax.swing.*; public
class W4E17 { public static double pow(double a,double
x) { return
W4E15.exp(x*W4E16.log(a)); } public static void main( String args[] ) { // a^x x>0
String sa=JOptionPane.showInputDialog("a
= "); double a = Double.parseDouble(sa);
String sx=JOptionPane.showInputDialog("x
= "); double x = Double.parseDouble(sx); String s=a+"^"+x+" =
"+pow(a,x)+" Math library value = "+Math.pow(a,x); JOptionPane.showMessageDialog(null,s); } // end method
main } // end class |
import javax.swing.*; public
class W4E17A { public static void main( String args[] ) { // a^x x>0
double a = W4E3A.read("a =
"); double x = W4E3A.read("x =
"); String s=a+"^"+x+" =
"+W4E17.pow(a,x)+"\nMath library :"+Math.pow(a,x); W4E1.print(s,"pow"); } // end method
main } // end class |
class W4E18 { public
static String unicode(char x1,char x2) {String
s=""; int i=1; for(char b=x1;b<=x2;b++) {s+=b+" ";if(i%10==0)
s+="\n";i++;} return s; } public static void main(String args[]) { char c1='\u2200'; char c2='\u22F1'; JOptionPane.showMessageDialog(null,unicode(c1,c2),"======Unicode
Letters======",JOptionPane.PLAIN_MESSAGE); } } |
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Following example finds prime numbers.
import javax.swing.*; class
prime4 { public
static String prime(int n1,int n2) { int i,j; String s=" "; for(i=n1;i<=n2;i++) {for(j=2;j<i
&& (i%j)!=0;j++) {} if(i==j) {s+=i+"\n";} } return s; }
public static void main(String args[]) { int n1=Integer.parseInt(JOptionPane.showInputDialog("enter
first number : ")); int n2=Integer.parseInt(JOptionPane.showInputDialog("enter
second number : "));
JOptionPane.showMessageDialog(null,prime(n1,n2),"Prime numbers
between "+n1+" and "+n2,JOptionPane.PLAIN_MESSAGE); } } |
HOMEWORKS (You will return
solutions of this sets as homework)
Utilise static methods for each
homework problem
HW1 : Enter the season as a String variable,
if it is fall write down “it is the season of golden trees” , if it is
winter write down “it is the season of skiing”,
if it is spring write down “it is the season of rebirth of the natüre”,
and if it is summer write down “it is the golden summer”
HW 2: write a program to calculate
factorial of an integer number (multiplication of numbers 1 to n)
n!=1*2*3*4..*n.
a)
Use while loop
b)
Use for loop
(you will write two version of
the program first with a while loop, second with a for loop)
HW
3 : When a complex numbers such
as p=3.141592653589793, series
solution is used. A series
can
be used. Write a static method
public static double
PI() to calculate the given series by using a while loop.
HW 4:
When a function such as sinh is
required to calculate by computer, series solution is used. A series
can
be used to calculate such a function. Write a static method public static double sinh(double x) to
calculate the
given
series by using a for loop.
Note: you can check
your result by using Math.sinh(x) library static
method.