W3 COMPUTER PROGRAMMINING 2020 SPRING

 

 CLASS EXERCISES

Class exercises will be completed and graded in class

Eksersiz programları yazılacak ve çalıştırılacak, sınıf hocasına gösterilecektir.

 

W3Ex1

import javax.swing.JOptionPane;

class W3Ex1

{

public static void main(String arg[])

{ String s=JOptionPane.showInputDialog("n = ");

  int n=Integer.parseInt(s);

  int total=0;

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

  {total+=i;}

  String w="n = "+n+" total = "+total;

  JOptionPane.showMessageDialog(null,w);

 }

}

 

W3Ex1A

 import javax.swing.JOptionPane;

class W3Ex1A

{

public static void main(String arg[])

{ String s=JOptionPane.showInputDialog("n = ");

  int n=Integer.parseInt(s);

  int total=0;

  int i=0;

  while(i<=n)

  {total+=i;i++;}

  String w="n = "+n+" total = "+total;

  JOptionPane.showMessageDialog(null,w);

}

}

 

W3Ex1B

import javax.swing.JOptionPane;

class W3Ex1B

{

public static void main(String arg[])

{ String s=JOptionPane.showInputDialog("n = ");

  double n=Integer.parseInt(s);

  double total=0;

  int i=0;

  do

  {total+=i;i++;}while(i<=n);

  String w="n = "+n+" total = "+total;

  JOptionPane.showMessageDialog(null,w);

}

}

 

 

W2Ex1C

 import javax.swing.JOptionPane;

class W3Ex1C

{

public static int sum(int n)

{ int total=0;

  int i=0;

  while(i<=n)

  {total+=i;

   i++;}

  return total;

}

 

public static void main(String arg[])

{ String s=JOptionPane.showInputDialog("n = ");

  int n=Integer.parseInt(s);

  String w="n = "+n+" total = "+sum(n);

  JOptionPane.showMessageDialog(null,w);

}

}

 

W3Ex1D

 import javax.swing.JOptionPane;

class W3Ex1D

{

public static int sum(int n)

{ int total=0;

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

  {total+=i;}

  return total;

}

public static int readI(String s)

{ String w=JOptionPane.showInputDialog("n = ");

  int n=Integer.parseInt(w);

  return n;

}

public static void print(String s)

{JOptionPane.showMessageDialog(null,s);}

 

public static void main(String arg[])

{ int n=readI("n = ");

  double total=sum(n);

  String w="n = "+n+" total = "+total;

  print(w);

}}

 

W3Ex2       character variables

import javax.swing.JOptionPane;

public class W3Ex2

{ public static String char_list(char first_char,char last_char)

 {String s="";

 for(char b=first_char;b<=last_char;b++)

 {s+=b+" ";}

 return s;

 }

public static void print(String s)

{JOptionPane.showMessageDialog(null,s);}

 public static void main(String arg[])

 {char c1='A';

  char c2='Z';

 String s=char_list(c1,c2);

 print(s);

 }

}

W3EX2A

import javax.swing.JOptionPane;

public class W3Ex2A

{

 public static void main(String arg[])

 {char c1='\u03B1';

  char c2='\u03FF';;

 String s=W3Ex2.char_list(c1,c2);

 W3Ex2.print(s);

 }

}

 

W3Ex3

 import javax.swing.JOptionPane;

public class W3Ex3

{ public static int f(int x)

 {return x*x+2*x+1;}

 public static void plot(int xmin,int xmax)

 { int x=xmin;

 String sum="";

 String s="";

 int y=0;

 while(x<=xmax)

 { y=f(x); // function to be plotted

 int i=0;

 sum="*";

 while(i<y)

 { sum = " "+sum;i++;}

 s+=sum+"\n";

 x++;

 }

 JOptionPane.showMessageDialog(null,s);

}

 public static void main(String arg[])

 { plot(-10,10);}

}

 

W3Ex3A

 import javax.swing.JOptionPane;

public class W3Ex3A

{ public static int f(int x)

 {return x*x+2*x+1;}

 public static void plot(int xmin,int xmax)

 {

 String sum="";

 String s="";

 int y=0;

 for(int x=xmin;x<=xmax;x++)

 { y=f(x); // function to be plotted

 sum="*";

 for(int i=0;i<y;i++) { sum =" "+sum;}

 s+=sum+"\n";

 }

 JOptionPane.showMessageDialog(null,s);

}

 public static void main(String arg[])

 { plot(-10,10);}

}

 

W3Ex4

 import javax.swing.JOptionPane;

public class W3Ex4

{ public static double exp(double x)

 { double power=1;

 double exponent=1;

 double factorial=1;

 double number=1;

 while(number<=200)

 {

 factorial*=number;

 power*=x;

 exponent+=power/factorial;

 number++;

 }

 return exponent;

 }

 public static void main(String arg[])

 { double x=1.0;

 String s="exp("+x+") = "+exp(x)+"\n";

 s+="Math.exp("+x+") = "+Math.exp(x)+"\n";

 JOptionPane.showMessageDialog(null,s);

 }

}

 

W3Ex4A

import javax.swing.JOptionPane;

public class W3Ex4A

{public static double exp(double x)

 { double power=1;

 double exponent=1;

 double factorial=1;

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

 { factorial*=number;

   power*=x;

   exponent+=power/factorial;

 }

 return exponent;

 }

 public static void main(String arg[])

 { double x=1.0;

 String s="exp("+x+") = "+exp(x)+"\n";

 s+="Math.exp("+x+") = "+Math.exp(x)+"\n";

 JOptionPane.showMessageDialog(null,s);

 }}

 

HOMEWORK EXERCISES

Homework exercises will be done at home and will bring to next tuesday class printed no late exercises will be excepted. Each code should include student name id#, code plus results should be given. Homeworks will be accepted in written format only

 

W3HW1

By using Unicode characters complete the following program so that output will be:

 

import javax.swing.JOptionPane;

public class W3HW1

{ public static String char_list(int n,char first_char,char last_char)

 {String s="";

 int i=n;

 for(char b=first_char;b<=last_char;b++)

 {....................... }

 return s;

 }

 public static void main(String arg[])

 {char c1='\u2230';

  char c2='\u2240';

  int n=2230;

 String s=char_list(n,c1,c2);

 W3Ex2.print(s);

 }

}

 

W3HW2  (if statement )

Function is given write a method public static void plot(int xmin,int xmax) to plot function see program W3Ex3

import javax.swing.JOptionPane;

public class W3HW2

{  public static int f1(int x)

   { int y=0;

     if(x<0){y=x*x-3*x+14;}

     else   {y=2*x+14;}

     return y;

   }

 public static void plot(int xmin,int xmax)

 {......}

 

}

   public static void main(String arg[])

   {plot(-10,10);}

}

 

W1HW3 Following series formula is given:

The following program is given

import javax.swing.JOptionPane;

public class W3HW3

{ public static double ln(double x)

{ // -0.5 < x <0.5

 double y=x-1;

 double power=1;

 double total=0;

 if(x<-0.5 && x>0.5)

 {System.out.println("Error: equation is out of range");return 0.0;}

 else

 {

 int plusminus=1;

 for(int n=1;n<=200;n++)

 { power*=y;

 total+=power/n*plusminus;

 plusminus*=-1;

 }

 return total;

 }

 }

public static void main(String arg[])

{ double x=0.5;

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

s+="Math.log("+x+") = "+Math.log(x)+"\n";

JOptionPane.showMessageDialog(null,s);

}

}

Investigate program, then change line double x=0.5; to an outside input statement then calculate

ln(-0.4)  ln(-0.3) ln(-0.2) ln(0.2) ln(0.4) by using your new program

W1HW4

Write a program to calculate the number by using a for loop. They claim that number A is equal to   =9.86969604401 check if it is true.

 

public class W3HW4

{ public static double A()

  { ......}

public static void main(String arg[])

{ double A=A();

String s="A = "+A+"\n";

s+="Pi^2 = "+Math.PI*Math.PI+"\n";

System.out.println(s);

}

}

 

Summary of Java Statements

Console Input

Scanner input = new Scanner(System.in);

int intValue = input.nextInt();

long longValue = input.nextLong();

double doubleValue = input.nextDouble();

float floatValue = input.nextFloat();

String string = input.next();

Console Output

System.out.println(anyValue);

GUI Input Dialog

String string = JOptionPane.showInputDialog(

"Enter input");

int intValue = Integer.parseInt(string);

double doubleValue =

Double.parseDouble(string);

Message Dialog

JOptionPane.showMessageDialog(null,

"Enter input");

 

Primitive Data Types

byte 8 bits (from -128 to 127)

short 16 bits (From -32768 to 32767)

int 32 bits (From -2157483648 to 2147483647)

long 64 bits (From -9223372036854775808 to  9223372036854775808)

float 32 bits  (From -3.40292347e+38 to 3.40292347e+38)

double 64 bits (From 1.7976931348623157e+308 to 
1.7976931348623157e+308)

char 16 bits  (Unicode)

boolean 1 bit (true/false)

Arithmetic Operators

+ addition

- subtraction

* multiplication

/ division

% remainder

++var preincrement

--var predecrement

var++ postincrement

var-- postdecrement

Assignment Operators

= assignment

+= addition assignment

-= subtraction assignment

*= multiplication assignment

/= division assignment

%= remainder assignment

Relational Operators

< less than

<= less than or equal to

> greater than

>= greater than or equal to

== equal to

!= not equal

Logical Operators

&& short circuit AND

|| short circuit OR

! NOT

^ exclusive OR

if (condition1) {statements;}

else if (condition2) {statements;}

else if (condition3) {statements;}

………

else {statements;}

 

switch Statements

switch (intExpression) {

case value1:

statements;

break;

...

case valuen:

statements;

break;

default:

statements;

}

While and do-while loop Statements

 

while (condition) {

statements;

}

 

 

do {

statements;

} while (condition);

For loop statements

 

for (init; condition;adjustment)

{

statements;

}

 

Frequently Used Static Constants/Methods

 

Math.PI

Math.exp()

Math.random()

Math.pow(a, b)

System.currentTimeMillis()

System.out.println(anyValue)

JOptionPane.showMessageDialog(null,

message)

JOptionPane.showInputDialog(

prompt-message)

Integer.parseInt(string)

Double.parseDouble(string)

Arrays.sort(type[] list)

Arrays.binarySearch(type[] list, type key)

Array/Length/Initializer

 

int[] list = new int[10];

list.length;

int[] list = {1, 2, 3, 4};

 

Multidimensional Array/Length/Initializer

int[][] list = new int[10][10];

list.length;

list[0].length;

int[][] list = {{1, 2}, {3, 4}};

 

 

Table 2.2.3 Some unicode character tables