The 1 st tutoring session of CSc2310 Fall, 2012
description
Transcript of The 1 st tutoring session of CSc2310 Fall, 2012
![Page 1: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/1.jpg)
The 1st tutoring session of CSc2310Fall, 2012
Haidong Xue
![Page 2: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/2.jpg)
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!");
}}
Hellow World!
Code:
Output:
![Page 3: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/3.jpg)
How to compile and run a java program (in command-line)?
• 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)
public class HelloWorld {public static void main(String[] args) {
System.out.println("Hellow World!");
}}
![Page 4: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/4.jpg)
How to compile and run a java program (in command-line)?
![Page 5: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/5.jpg)
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 valuepublic class Variables {
public static void main(String[] args) {String name = "Haydon";boolean male = true;double height = 1.76; //metersSystem.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);
}}
![Page 6: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/6.jpg)
Use variablespublic class Variables {
public static void main(String[] args) {String name = "Haydon";boolean male = true;double height = 1.76; //metersSystem.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);
}}
Hello World!I am: HaydonAm I a male: trueMy height is: 1.76
Code:
Output:
![Page 7: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/7.jpg)
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
![Page 8: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/8.jpg)
Use operatorspublic class Operators{
public static void main(String[] args) {double radius = 3.5;double area = 3.1415926 * radius * radius;System.out.println("Area: " + area);
}}
Area: 38.48450935
Code:
Output:
![Page 9: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/9.jpg)
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
![Page 10: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/10.jpg)
Use constantspublic 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?
![Page 11: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/11.jpg)
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.
![Page 12: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/12.jpg)
Use methods• Methods are members of classes• You can use the “.” operator to use the public
methodspublic 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.01.41421356237309512
![Page 13: The 1 st tutoring session of CSc2310 Fall, 2012](https://reader036.fdocuments.in/reader036/viewer/2022082819/56813ad5550346895da30ca5/html5/thumbnails/13.jpg)
If you have more questions, I will be here till 8:30pm