W1 COMPUTER PROGRAMMINING 2020 SPRING

Grading:

Class exercises : %15

Homework exercises %15

Midterm exam: %30

Final exam (written in class exam ) open book %40

Books:

http://www.turhancoban.com/kitap/COMPUTER%20PROGRAMMING.pdf

Java How to Program, 11/e (Early Objects) Deitel 2018 ISBN 0-13-474335-0

 

 

 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.

 

EX1

program1.java

class program1

{public static void main(String arg[])

 { System.out.println("Welcome to Java");}

}

program1A.java

import javax.swing.JOptionPane;

class program1A

{public static void main(String arg[])

 { JOptionPane.showMessageDialog(null,"Welcome to Java");}

}

program1B.java

import javax.swing.JOptionPane;

class program1B

{public static void print(String s)

 { System.out.println(s);}

 public static void main(String arg[])

 { print("Welcome to Java");}

}

program1C.java

import javax.swing.JOptionPane;

class program1C

{public static void print(String s)

 { JOptionPane.showMessageDialog(null,s);}

 public static void main(String arg[])

 { print("Welcome to Java");}

}

program1D.java

import javax.swing.JOptionPane;

class program1D

{ public static void main(String arg[])

  { program1B.print("Welcome to Java");}

}

 

EX2

program2.java

class program2

{public static void main(String arg[])

 { double x=1.234;

   double y=2.23;

 System.out.println("x="+x+"y="+y+"x*y="+x*y);}

}

 

program2A.java

class program2A

{public static void main(String arg[])

 { double x=1.234;

   double y=2.23;

   double z=x*y;

   program1B.print("x = "+x+" y = "+y+"\nx*y = "+z);}

}

 

program2B.java

import javax.swing.JOptionPane;

class program2B

{ public static void print(String s)

{ JOptionPane.showMessageDialog(null,s);}

public static void main(String arg[])

{ int x=5;

  int y=2;

  String s="x/y = "+(x/y)+"\n"+"x%y = "+(x%y);

  print(s);

}

}

 

EX3

program3.java

import javax.swing.JOptionPane;

class program3

{ public static void print(String s)

 {JOptionPane.showMessageDialog(null,s);}

public static void main(String arg[])

{ double x=Double.parseDouble(JOptionPane.showInputDialog("x="));

  double y=Double.parseDouble(JOptionPane.showInputDialog("y="));;

  String s="x/y = "+(x/y);

  print(s);}

}

program3A.java

import javax.swing.JOptionPane;

class program3A

{

public static void main(String arg[])

{ int x=Integer.parseInt(JOptionPane.showInputDialog("x="));

  int y=Integer.parseInt(JOptionPane.showInputDialog("y="));;

  String s="x/y = "+(x/y)+"\nx%y = "+(x%y);

  program3.print(s);}

}

program3B

import javax.swing.JOptionPane;

class program3B

{

public static String read(String s)

{String x=JOptionPane.showInputDialog(s);

 return x;

}

public static int read_int(String s)

{   String s1=read(s);

            int x=Integer.parseInt(s1);

    return x;

}

public static void main(String arg[])

{ int x=read_int("x=");

  int y=read_int("y=");

  String s="x/y = "+(x/y)+"\nx%y = "+(x%y);

  program3.print(s);}

}

program3C.java

class program3C

{

public static double read_double(String s)

{   String s1=program3B.read(s);

            double x=Double.parseDouble(s1);

    return x;

}

public static void main(String arg[])

{ double x=read_double("x=");

  double y=read_double("y=");

  String s="x/y = "+(x/y);

  program3.print(s);}

}

program3D.java

class program3D

{

public static void main(String arg[])

{ String name=program3B.read("What is your name? ");

  String s="welcome to java class "+name;

  program3.print(s);}

}

program3E.java

import java.util.Scanner;

class program3E

{

public static void print(String s)

{System.out.println(s);}

public static String input(String s)

{ // create Scanner to obtain input from command window

Scanner input = new Scanner( System.in );

System.out.print(s);

String x=input.next();

return x;

}

public static void main(String arg[])

{ String name=input("enter your name =");

String last_name=input("enter your last name =");

String id=input("enter your student id# =");

String s="Your identification : \nname : "+name+" "+last_name+"\n Student id# : "+id;

print(s);

}

}

program3F.java

class program3F

{

public static void main(String arg[])

{ double x=program3C.read_double("x=");

  double y=x*x-2.3*x+5.2;

  String s="x = "+x+" y = "+y;

  program3.print(s);}

}

program3G.java

class program3G

{

public static double f(double x)

{  double y=x*x-2.3*x+5.2;

   return y;

}         

public static void main(String arg[])

{ double x=program3C.read_double("x=");

  String s="x = "+x+" y = "+f(x);

  program3.print(s);}

}

 

HOMEWORK EXERCISES

Homework exercises will be done at home and will bring to next Thursday 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 plus a computer copy in pdf format will be sent to computer_programming@turhancoban.com adress your file name should be “group”+“week#”+studentname+studentid#.pdf

A_W1_turhan_coban_0101333.pdf

B_W3_ali_veli_02335646.pdf

 

W1HW1

y=f(x)=3.1x2-1.23x+13.8 function is given. Write a program to calculate values for x=1.25,2.3 and 5.6

input and output should be graphic form-GUI (JOptionPane.showInputDialog, JOptionPane.showMessageDialog) see program3G

 

W1HW2

y=f(x)=3.1x2-1.23x+13.8 function is given. Write a program to calculate values for x=1.25,2.3 and 5.6

input should be from console screen form (Scanner, input.next, System.out.println)  see program3E

 

W1HW3

Enter your name from the screen as input by using graphic form and write “welcome to java”+your name as output

Example: Welcome to java Turhan

 

W1HW4

Enter your name from the screen as input by using console screen form (Scanner, input.next)

and write “welcome to java”+your name as output

Example: Welcome to java Turhan

 

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