The 1 st and 2 nd tutoring session of csc2310 fall 2012
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

The 1 st and 2 nd tutoring session of CSc2310 Fall, 2012 PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on
  • Presentation posted in: General

The 1 st and 2 nd tutoring session of CSc2310 Fall, 2012. Haidong Xue. How to compile and run a java program (in command-line)?. Code:. public class HelloWorld { public static void main(String[] args ) { System. out.println (" Hellow World!"); } }. Output:. Hellow World!.

Download Presentation

The 1 st and 2 nd tutoring session of CSc2310 Fall, 2012

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


The 1 st and 2 nd tutoring session of csc2310 fall 2012

The 1st and 2nd tutoring session of CSc2310Fall, 2012

HaidongXue


How to compile and run a java program in command line

How to compile and run a java program (in command-line)?

Code:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hellow World!");

}

}

Output:

Hellow World!


How to compile and run a java program in command line1

How to compile and run a java program (in command-line)?

public class HelloWorld{

public static void main(String[] args) {

System.out.println("Hellow World!");

}

}

  • Install JDK (not JRE)

  • Compile:

    javac HelloWorld.java

    (You will see the bytecode file: HelloWorld.class)

  • Run:

    java HelloWorld

    (not HelloWorld.class here)

    (All the file names are case-sensitive)


How to compile and run a java program in command line2

How to compile and run a java program (in command-line)?


Use variables

Use variables

  • Each variable has a type

  • Use a type keyword to declare a variable

  • In the most recent grammar, you have to initialize the variable with certain value

public class Variables {

public static void main(String[] args) {

String name = "Haydon";

booleanmale = true;

double height = 1.76; //meters

System.out.println("Hello World!");

System.out.println("I am: " + name);

System.out.println("Am I a male: " + male);

System.out.println("My height is: " + height);

}

}


Use variables1

Use variables

Code:

public class Variables {

public static void main(String[] args) {

String name = "Haydon";

booleanmale = true;

double height = 1.76; //meters

System.out.println("Hello World!");

System.out.println("I am: " + name);

System.out.println("Am I a male: " + male);

System.out.println("My height is: " + height);

}

}

Output:

Hello World!

I am: Haydon

Am I a male: true

My height is: 1.76


Use operators

Use operators

  • There are many operators, their usage is very similar to their counterparts in mathematics.

  • E.g.:

    +Addition

    -Subtraction

    *Multiplication

    /Division

  • There are also many other operators like: modulus(%), increment(++) and so on


Use operators1

Use operators

Code:

public class Operators{

public static void main(String[] args) {

double radius = 3.5;

double area = 3.1415926 * radius * radius;

System.out.println("Area: " + area);

}

}

Output:

Area: 38.48450935


Use constants

Use constants

  • Like a variable, but the value cannot be changed.

    (You can only initialize it)

  • It is very useful to replace your “magic numbers” with constants to increase the readability of your code


Use constants1

Use constants

public class Operators{

public static void main(String[] args) {

double radius = 3.5;

double area = 3.1415926 * radius * radius;

System.out.println("Area: " + area);

}

}

public class Constants {

public static void main(String[] args) {

final double PI = 3.1415926;

double radius = 3.5;

double area = PI * radius * radius;

System.out.println("Area: " + area);

}

}

Which one is better?


Use constants2

Use constants

  • What will happen, if you use “3.2415” as PI at multiple places, and suddenly find out that it should be “3.1415”?

  • When you read a 2000 line program, what if you see 8.314?

  • To avoid those pains, coders use constants.


Use methods

Use methods

  • Methods are members of classes

  • You can use the “.” operator to use the public methods

public class Methods {

public static void main(String[] args) {

double value = 2;

System.out.println(Math.pow(value, 3)); //

System.out.println(Math.sqrt(value)); //

System.out.println(Math.round(value)); // round(value)

}

}

8.0

1.4142135623730951

2


If you have more questions i will be here till 8 30pm

If you have more questions, I will be here till 8:30pm


  • Login