COMPUTER PROGRAMMING 2014 WEEK 3

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 W3E1

 {    public static void main(String args[])

      {    String weather;

           weather=JOptionPane.showInputDialog("wheather condition: clear/rainy/cloudy");

           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";}

          JOptionPane.showMessageDialog(null,s,"======If statement======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

import javax.swing.*;

class W3E2

 {    public static void main(String args[])

      {    String weather;

           weather=JOptionPane.showInputDialog("wheather condition:\n 1:clear\n 2:rainy\n 3:cloudy");

           int n=Integer.parseInt(weather);

           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";}

          JOptionPane.showMessageDialog(null,s,"======If statement======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

 import javax.swing.*;

public class W3E3

{

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=b*b-4.0*a*c;

    boolean b1=d>0;

    boolean b2=d<0;

    boolean b3=d==0;

    String s="delta="+b+"^2-4*"+a+"*+"+c+" = "+d+"\n";

    if(d>0)

    {s+="determinant greater than 0 \nthe roots are real";}

    else if(d<0)

    {s+="determinant smaller than 0\nthe roots are complex";}

    else

    {s+="determinant are equal to 0\ntwo equal real roots";}  

            JOptionPane.showMessageDialog(null,s);

}

}

 

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 W3E4

 {    public static void main(String args[])

      {    String weather;

           weather=JOptionPane.showInputDialog("wheather condition:\n 1:clear\n 2:rainy\n 3:cloudy");

           //s2 defines integer range in unicode string

           String s2="^-?[0-9]+(\\.[0-9]+)?$";

           boolean b=weather.matches(s2);

           int n=0;

           String s;

           if(b)

           {n=Integer.parseInt(weather);        

              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";}

           }

           else

           {s="the given input is not an integer";}

          JOptionPane.showMessageDialog(null,s,"======If statement======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

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 W3E5

 {    public static void main(String args[])

      {    String s1;

           //s2 defines double variable in unicode char Strings

           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]*";

           s1=JOptionPane.showInputDialog("enter a double number : ");

           boolean b=s1.matches(s2) ;

           String s;

           double n;

           if(b)

           {n=Double.parseDouble(s1);        

            s="double number is "+n;

           }

           else

           {s="the given input is not a double number";}

          JOptionPane.showMessageDialog(null,s,"======If statement======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

WHILE STATEMENT

while( boolean variable)

{ processes while boolean variable is true}

 

 

 import javax.swing.*;

class W3E6

 {    public static void main(String args[])

      {    int x=0;

           int x1=20;

           int y;

           String s="";

           String space=" ";

           while(x<x1)

           {y=x*x+2*x+1;

            x++;

            s+="x = "+x+" y = "+y+"\n";

           }

          JOptionPane.showMessageDialog(null,s,"====== function evaluation ======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

 import javax.swing.*;

class W3E7

 {    public static void main(String args[])

      {    //sum of the numbers from 1 to 20

                   int x=1;

           int x1=20;

           int sum=0;

           String s="";

           String space=" ";

           while(x<x1)

           {sum+=x;

            x++;

           }

           s+=" sum = "+sum+"\n";

          JOptionPane.showMessageDialog(null,s,"====== function evaluation ======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

 import javax.swing.*;

class W3E8

 {    public static void main(String args[])

      { String star="*";

        String space=" ";

        String sum;

        String s="";

        int x=-10;

        int x2=10;

        int y=0;

        while(x<=x2)

        {   y=x*x+2*x+1;  // function to be plotted

            int i=0;

            sum=star;

            while(i<y)

            { sum = space+sum;

              i++;

            }

            s+=sum+"\n";

            x++;

        }

        JOptionPane.showMessageDialog(null,s,"====== function plot ======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

In the next example a more series will be calculated

note that e=2.7182818

 import javax.swing.*;

 

public class W3E9

{

     // main method begins execution of Java application

     public static void main( String args[] )

     {           

        // number e = 2.718...

        // exp(x) = 1 + x /1! + x2 / 2! + x3 / 3! + x4 / 4! + ..

        String sa=JOptionPane.showInputDialog("x=");

        double x = Double.parseDouble(sa);      

        double power=1;

        double exponent=1;

        double factorial=1;

        double number=1;

        while(number<=300)

        {

        factorial*=number;

        power*=x;

        exponent+=power/factorial;

        number++;

        }

        String s="exp("+x+") = "+exponent;

        JOptionPane.showMessageDialog(null,s);

     } // end method main

} // end class BPIH4E1

 

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 W3E10

{

     // 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);      

        double y=(x-1.0)/(x+1.0);

        double power=2.0*y;

        double z=y*y;

        double n=0;

        double ln=0;

        while(n<=300)

        {

        ln+=1./(2.0*n+1.0)*power;

        power*=z;

        n++;

        }

        String s="ln("+x+") = "+ln;

        JOptionPane.showMessageDialog(null,s);

     } // end method main

} // end class BPIH4E1

 

          (This program combines two previous programs to calculate power function

 import javax.swing.*;

 

public class W3E11

{    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(sa);

        //ln(a)     

        double y=(a-1.0)/(a+1.0);

        double power=2.0*y;

        double z=y*y;

        double n=0;

        double ln=0;

        while(n<=300)

        {

        ln+=1./(2.0*n+1.0)*power;

        power*=z;

        n++;

        }

        //exp function exp(x*ln(a))

        double w=x*ln;

        double power1=1;

        double exponent=1;

        double factorial=1;

        double number=1;

        while(number<=300)

        {

        factorial*=number;

        power1*=w;

        exponent+=power1/factorial;

        number++;

        }

        String s=a+"^"+x+" = "+exponent;

        JOptionPane.showMessageDialog(null,s);

     } // end method main

} // end class

FOR STATEMENT

for(initial statements; boolean condition; incremental statements)

{ processes while boolean variable is true}

 

 import javax.swing.*;

class W3E12

 {    public static void main(String args[])

      {   

           int x1=20;

           int y;

           String s="";

           String space=" ";

           for(int x=0;x<x1;x++)

           {y=x*x+2*x+1;

            s+="x = "+x+" y = "+y+"\n";

           }

          JOptionPane.showMessageDialog(null,s,"====== function evaluation ======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

 import javax.swing.*;

class W3E13

 {    public static void main(String args[])

      {    //sum of the numbers from 1 to 20

                  

           int x1=20;

           int sum=0;

           String s="";

           String space=" ";

           for(int x=1;x<x1;x++)

           {sum+=x;}

           s+=" sum = "+sum+"\n";

          JOptionPane.showMessageDialog(null,s,"====== sum of numbers ======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

 import javax.swing.*;

class W3E14

 {    public static void main(String args[])

      { String star="*";

        String space=" ";

        String sum;

        String s="";      

        int x2=10;

        int y=0;

        for(int x=-10;x<=x2;x++)

        {   y=x*x+2*x+1;  // function to be plotted         

            sum=star;

            for(int i=0;i<y;i++)

            { sum = space+sum;}

            s+=sum+"\n";           

        }

        JOptionPane.showMessageDialog(null,s,"====== function plot ======",JOptionPane.PLAIN_MESSAGE);

     }

 }

 

  import javax.swing.*;

 public class W3E15

{    // main method begins execution of Java application

     public static void main( String args[] )

     {  // number e = 2.718...

        // exp(x) = 1 + x /1! + x2 / 2! + x3 / 3! + x4 / 4! + ..

        String sa=JOptionPane.showInputDialog("x=");

        double x = Double.parseDouble(sa);      

        double power=1;

        double exponent=1;

        double factorial=1;      

        for(int number=1;number<=300;number++)

        {factorial*=number;

         power*=x;

         exponent+=power/factorial;      

        }

        String s="exp("+x+") = "+exponent+"  ";

        JOptionPane.showMessageDialog(null,s);

     } // end method main

} // end class BPIH4E1

 

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

 import javax.swing.*;

 

public class W3E16

{

     // 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);      

        double y=(x-1.0)/(x+1.0);

        double power=2.0*y;

        double z=y*y;     

        double ln=0;

        for(int n=0;n<=300;n++)

        { ln+=1./(2.0*n+1.0)*power;

          power*=z;       

        }

        String s="ln("+x+") = "+ln;

        JOptionPane.showMessageDialog(null,s);

     } // end method main

} // end class

 

 import javax.swing.*;

 

public class W3E17

{    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(sa);

        //ln(a)     

              double y=(x-1.0)/(x+1.0);

             double power=2.0*y;

             double z=y*y;     

             double ln=0;

             for(int n=0;n<=300;n++)

                 { ln+=1./(2.0*n+1.0)*power;

                    power*=z;       

                 }

        //exp function exp(x*ln(a))

        double w=x*ln;

             double power1=1;

             double exponent=1;

             double factorial=1;      

             for(int number=1;number<=300;number++)

             {

             factorial*=number;

             power1*=w;

             exponent+=power1/factorial;       

             }

         String s=a+"^"+x+" = "+exponent;

        JOptionPane.showMessageDialog(null,s);

     } // end method main

} // end class

 

import java.io.*; //java girdi cikti sinifini cagir

import javax.swing.*;

 

class W3E18

 {    public static void main(String args[])

      {

          String s="";

          for(char b='A';b<='Z';b++)

          {s+=b+" ";}

          JOptionPane.showMessageDialog(null,s,"======English 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 prime3

{               

      public static void main(String args[])

      {   

           int n1=Integer.parseInt(JOptionPane.showInputDialog("enter first number  : "));

           int n2=Integer.parseInt(JOptionPane.showInputDialog("enter second number : "));

           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";}

           }          

           JOptionPane.showMessageDialog(null,s,"Prime numbers between "+n1+" and "+n2,JOptionPane.PLAIN_MESSAGE);        

     }

 }

 

HOMEWORKS (You will return solutions of this sets as homework)

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 nature”, 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 program 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 program to calculate the given series by using a for loop.