W2 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.
W2EX1
import javax.swing.JOptionPane; class W2Ex1 { public static void main(String arg[]) { String s=JOptionPane.showInputDialog("x="); double x=Double.parseDouble(s); double y=3.1*x*x-1.23*x+13.8; String w="x =
"+x+" y = "+y; System.out.println(w);} } |
W2Ex1A
import javax.swing.JOptionPane; class W2Ex1A { public static double f(double x) {double y=3.1*x*x-1.23*x+13.8; return y; } public static void main(String arg[]) { String s=JOptionPane.showInputDialog("x
= "); double x=Double.parseDouble(s); String w="x =
"+x+" y = "+f(x); JOptionPane.showMessageDialog(null,w);} } |
W2Ex1B
import javax.swing.JOptionPane; class W2Ex1B { public static double f(double x) {double y=3.1*x*x-1.23*x+13.8; return y; } public static double readD(String s) {String z=JOptionPane.showInputDialog(s); double x1=Double.parseDouble(z); return x1; } public static void print(String w) {JOptionPane.showMessageDialog(null,w);} public static void main(String arg[]) { double x=readD("x
= "); String s="x =
"+x+" y = "+f(x); print(s); } } |
W2Ex1C
import javax.swing.JOptionPane; class W2Ex1C { public static double f(double x) {double y=3.1*x*x-1.23*x+13.8; return y; } public static void main(String arg[]) { double x=W2Ex1B.readD("x = "); String s="x =
"+x+" y = "+f(x); W2Ex1B.print(s); }} |
W2Ex1D
import java.util.Scanner; class W2Ex1D { public static void main(String arg[]) { Scanner input = new Scanner( System.in ); System.out.print("x="); String s=input.next(); double x=Double.parseDouble(s); double y=3.1*x*x-1.23*x+13.8; String w="x =
"+x+" y = "+y; System.out.println(w);} } |
Character (char) variable type define actual characters in computers. In order to see
characters in java ISO Unicode character
standards are used. Unicode character code consist of 4 hexagonal (base 16) number. In case
you are not familiar with orthogonal number system, a table of equiavalancy is given Tablo 2.2.1 Heksagonal(sixteen base), and 10 base and binary
number systems equvalency
in ISO unicode '\u0041' defines character 'A'. or '\u03E1'. Or code '\u03E1' defines 'a'. When the first two
number of unicode equals to 0, it defines ASCII characte codes. You can find very detailed
list of characters in ISO
unicode by looking the internet site http:\\unicode.org |
W2Ex2 character variables
import javax.swing.*; public class W2Ex2 { public static void main(String args[]) { char
b1,b2,b3,b4,b5,b6; b1='ü'; b2='ğ'; b3='ç'; b4='a'; b5='b'; b6='c'; String
s=""+b1+b2+b3+b4+b5+b6; JOptionPane.showMessageDialog(null,s," character procesing",JOptionPane.PLAIN_MESSAGE); } } |
W2Ex2A
import javax.swing.*; public class W2Ex2A { public static void print(String
s) {JOptionPane.showMessageDialog(null,s," character procesing",JOptionPane.PLAIN_MESSAGE);} public static void main(String args[]) { char
b1,b2,b3,b4,b5,b6; b1='\u00FC'; b2='\u01E7'; b3='\u00E7'; b4='\u0061'; b5='\u0062'; b6='\u0063'; String
s=""+b1+b2+b3+b4+b5+b6; print(s); } } |
W2Ex2B
public class W2Ex2B { public static void main(String args[]) { char
b1,b2,b3; b1='\u03B1'; // alfa b2='\u03B2'; // beta b3='\u03B3'; // gamma String s="alfa = "+b1+" beta="+b2+"
gamma="+b3; W2Ex2A.print(s); } } |
W2Ex2C
public class W2Ex2C { // characters public static void main(String arg[]) { char
b1,b2; b1='\u0394'; //big delta b2='\u00B2'; //Square sign String
s=""+b1+"T ="+b1+"x"+b2+" +
"+b1+"y"+b2; W2Ex2A.print(s); }} |
Boolean type variable is for logical processes. Key word for
the boolean type variable is boolean. Boolean variable is only one bit long and taken value
true or false boolean x=true; boolean y=false; The available operators for the boolean variables
are: Table 3.1.1 Boolean operators that operate directly in boolean variables
Table 3.1.3 Boolean operators that operates on integer and real
variables variables
|
W2Ex3 Boolean variables
import javax.swing.*; public class W2Ex3 { //boolean
variables public static void print(String s) {JOptionPane.showMessageDialog(null,s,"W2Ex3 Boolean variables",JOptionPane.PLAIN_MESSAGE);} public static void main(String arg[]) { boolean b1=true; boolean b2=false; boolean b3=b1&&b2; boolean b4=b1||b2; boolean b5=!b1; String s="b1="+b1+"\n"; s+="b2="+b2+"\n"; s+="b3="+b3+"\n"; s+="b4="+b4+"\n"; s+="b5="+b5+"\n"; print(s); } } |
W2Ex3A
import javax.swing.*; public class W2Ex3A { //boolean
variables public static int readI(String s) { String w=JOptionPane.showInputDialog(s); int
x=Integer.parseInt(w); return x; } public static void main(String arg[]) { int x=readI("x = "); int y=readI("y = "); boolean b1=x>y; boolean b2=x<y; boolean b3=(x==y); boolean b4=(x!=y); boolean b5=!(x==y); boolean b6=(x==y) && (x!=y); boolean b7=b3 && b4; String s="b1="+b1+"\n"; s+="b2="+b2+"\n"; s+="b3="+b3+"\n"; s+="b4="+b4+"\n"; s+="b5="+b5+"\n"; s+="b6="+b6+"\n"; s+="b7="+b7+"\n"; W2Ex3.print(s); }} |
W2Ex4 If statement
import javax.swing.*; public class W2Ex4 { //If statement public static void print(String s) {JOptionPane.showMessageDialog(null,s,"W2Ex4 If statement",JOptionPane.PLAIN_MESSAGE);} public static String read(String s) {String w=JOptionPane.showInputDialog(s);return w;} public static void main(String arg[]) {String x=read("enter your name"); String t="Turhan"; boolean
b1=x.equals(t); String s=""; if(b1) {s="your name is
"+t;} else {s="your name is not
"+t;} print(s); }} |
W2Ex4A.java
import java.io.*; import javax.swing.JOptionPane; class W2Ex4A { public static double readD(String s) {String w=JOptionPane.showInputDialog(s);double d=Double.parseDouble(w);return
d;} public static void main(String args[]) { double x; double y; x=readD(" x = "); y=readD(" y = "); if(x<y) { W2Ex4.print(x+" is smaller than "+y);} else if(x>y) { W2Ex4.print(x+" is bigger than "+y);} else if(x==y) { W2Ex4.print(x+" is equal to "+y);} } } |
W2Ex4B
import java.io.*; import javax.swing.JOptionPane; class W2Ex4B { public static double readD(String s) {String w=JOptionPane.showInputDialog(s);double d=Double.parseDouble(w);return
d;} public static void main(String args[]) { //Guess the color String s1; String s=W2Ex4.read(" Enter a color = "); boolean b1=s.equals("red"); if(b1) {s1=" Correct guess color you enter is red";} else {s1="
color you enter is not red, it is "+s;} W2Ex4.print(s1); } } |
W2Ex4C
class W2Ex4C { public static void main(String args[]) { double x; double y=0; x=W2Ex4A.readD(" x = "); if(x<3) { y=3.0*x+3;;} else if(x>=3 && x<5) { y=12+(x-3)*(x-3);} else { y=16.0+2.0*x*x-5.0;;} W2Ex4.print("x = "+x+"\ny =
"+y); }} |
W2Ex4D
class W2Ex4D { public static double f(double x) {double y=0; if(x<3) { y=3.0*x+3;;} else if(x>=3 && x<5) { y=12+(x-3)*(x-3);} else { y=16.0+2.0*x*x-5.0;;} return y; } public static void main(String args[]) { double x; x=W2Ex4A.readD(" x = "); double y=f(x); W2Ex4.print("x = "+x+" y = "+y); }} |
W2Ex4E
class W2Ex4E { public static String f(double x,double y) { String s=""; if(x>5 && y>5) { s="x and
y are greater than 5.";} else if(x>5 && y==5) { s="x
and y are greater than 5.";} else if(x>5 && y<5) { s="x
is greater than 5 and y is smaller than 5";} else if(x<=5 && y>5) {
s="x is smaller or equal to five
y is greater than 5.";} else if(x<=5 && y==5) { s="x
is greater than 5 and y is equal to
5";} else if(x <= 5 && y<5) {
s="x is smaller or equal to five
y is smaller than 5."; } return s; } public static void main(String args[]) { double x,y; x=W2Ex4A.readD(" x = "); y=W2Ex4A.readD(" y = "); W2Ex4.print(f(x,y)); }} |
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
W2HW1
By using Unicode characters
write
Into graphic screen by using
JOptionPane.showMessageDialog
method
W2HW2 (if statement )
Function is given write a method public static double y(double x) to calculate the value see program W2Ex4C, W2Ex4E
W1HW3
Enter a name from the screen as
input
and if name is equal to “ali” or “veli” or “deli” write “you
are not welcome to java”+name
as output
and if not write “welcome to java”+name
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 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
|
|
|
|