cs.sbcc.educs.sbcc.edu/~rhd/javamidterm1b.docx  · web viewjava. enhanced multimedia capabilities...

12
Java Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on the Internet Multithreading Reusability - and existing libraries of code Portability - applications written on one platform will run on another Fully object oriented Java Applications public class HelloWorld { public static void main (String args[ ]) { System.out.println(“Hello World!”); } } Java Applets import java.awt.Graphics; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString(“Hello World!”, 5, 25); } }

Upload: others

Post on 13-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

JavaEnhanced multimedia capabilities incorporating images, animation audio and video.Enables applications to run on the InternetMultithreadingReusability - and existing libraries of codePortability - applications written on one platform will run on anotherFully object oriented Java Applications

public class HelloWorld{

public static void main (String args[ ]){

System.out.println(“Hello World!”);}

}

Java Appletsimport java.awt.Graphics;public class HelloWorldApplet extends Applet{

public void paint(Graphics g){

g.drawString(“Hello World!”, 5, 25);}

}

Page 2: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Variables - Name for a memory location:

Data_Type Variable_Name(userDefined) { = value} ;int number = 5;char ch = ‘A’; float f =1.3f;

Range of valuesint -2,147,483,648 to 2,147,483,647float -3.402823e38 to 3.402823e38double -1.79769313486232e308 to 1.79769313486232e308char Symbols used in text (8 bits)boolean true or false

byte -128 to 127short -32,768 to 32,767long Huge

Page 3: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Constants - A values that is fixed, explicitly stated, does not change:

final float SALESTAX = 0.0725;

taxOwed = 12 * SALESTAX;

Operator Precedence ChartYou may use this chart during quizzes and exams.

Example of Mod

int x = 42; x%10 2 x% 50 42

Page 4: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

The ++ and -- operators

Example 1int a = 1;int b = 2;int c;int d;

c = ++b; // c =3 b = 3d = a++; // d=1 a = 2c++; //c = 4

Example 2int a=4, b=9, c, d, e;c = ++a; //a = 5 c = 5d = b++; //d = 9 b = 10e = ++b; //b=11 e = 11

Example 3int num = 10;

(num++ = = num) evaluate? 10 == 11 False

(num = = num++) evaluate? 10 == 10 True After the execution of the statement the value of num changes to 11

(++num = = num) evaluate? 11 == 11 True

(num = = ++num) evaluate? 10 == 11 False

Page 5: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

package comparison;

/* Title: Comparison * Description: Compares 2 numbers *Copyright: Copyright Coach (c) 2014 * Company: Coach's Corporation * author Coach*/import java.awt.*;import java.awt.event.*;

public class ComparisonSwing extends Applet implements ActionListener{ Label prompt1; TextField input1; Label prompt2; TextField input2; int number1=0, number2=0; // setup the graphical user interface components and initialize variables public void init() { prompt1 = new Label("Enter an integer"); add(prompt1);

input1 = new TextField(10);add(input1);

prompt2 = new Label("Enter an integer and press Enter");add(prompt2);

input2 = new TextField(10); input2.addActionListener(this); add(input2); }//Display the resultpubic void paint (graphics g){

g.drawString(“Result”,100,100);g.drawString(“Number 1 is “ + number1,100,120);g.drawString(“Number 2 is “ + number2,100,140);

} // process user's action on the input2 text field public void actionPerformed(ActionEvent e) { number1 = Integer.parseInt( input1.getText() ); number2 = Integer.parseInt( input2.getText() );

repaint();}

Page 6: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Flow of execution of a source code.

Page 7: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

if Statementsint a, b, c;if (a<b)

c=a;else

c=b;//////////////////////////////////////////////////////////////////////////////////////int a,b,c,d;

if (a<b ){

if( a<c){

d = a;a = b;

{ }Else{

c=b;a=c;

}/////////////////////////////////////////////////////////////////////////////////////////////////

Conditional Expressionmax = (a>b) ? a: b;

if (a>b)max = a;

elsemax = b;

Another way of writing conditional expression(a>b)? max = a : max = b;

Page 8: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Relational Operators(used for comparison): = = ! = > < > = < =Logical Operators: && | |

A B A||B A&&B A^B !AT T T T F FT F T F T FF T T F T TF F F F F T

Page 9: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Loops

int n = 10;while (n > 0) {

System.out.println(“tick ” + n);n--;

}

for (int n=10; n>0; n--)System.out.println(“tick ” + n);

for (i=0; i<10; i++) {for (j=i; j<10; j++)

System.out.print(“.”);System.out.println();

}……….……………..…….………..….…..

for (int i=0; i<100; i++) {

if (i = = 10) break; // terminate if i is 10System.out.println( “i: ” + i);

}

Page 10: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Reserved word outer: for (int x=0; x<3; x++) {

System.out.print(“Pass ” + x + “: ”);for (int y=0; y<100; y++) {

if (y = = 10) break outer; // exit both loopsSystem.out.print(y + “ ”);

}System.out.println(“This will not print”);

}System.out.println(“Loops Complete.”);

for (int x=0; x<10; x++) {

System.out.print (“ “ + x ); if (x % 2 = = 0) continue;System.out.print (“”+x+ “ “ );

}0 11 233 455 677 899

Page 11: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

Examples of a switch statement:The parameter for switch statement should be either integer or character.

X = Integer.parsInt(input.getText());switch(x) {

case 0:System.out.println(“x is zero.”);break;

case 1:System.out.println(“x is one.”);break;

case 2:System.out.println(“x is two.”);break;

default:System.out.println(“x is not in range.”);

}

Page 12: cs.sbcc.educs.sbcc.edu/~rhd/JavaMidterm1b.docx  · Web viewJava. Enhanced multimedia capabilities incorporating images, animation audio and video. Enables applications to run on

int month;String season;

switch (month) {case 12:case 1:case 2:

season = “Winter”;break;

case 3:case 4:case 5:

season = “Spring”;break;

case 6:case 7:case 8:

season = “Summer”;break;

case 9:case 10:case 11:

season = “Autumn”;break;

default:season = “Bogus Month”;

}