csi 1390: introduction to computers ta: tapu kumar ghose office: ste 5014 email:...

13
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: [email protected]

Upload: jayson-wiggins

Post on 12-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

CSI 1390: Introduction to Computers

TA: Tapu Kumar GhoseOffice: STE 5014

Email: [email protected]

Page 2: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Create Folders and text files

Creating folder Navigate to the directory where the folder is to be

created Click right button of the mouse Point the mouse over the “New” item of the menu Click on the “Folder” option of the sub-menu Give a name to the newly created folder Press F2 to rename the folder name

Page 3: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Create Folders and text files Creating text file using “notepad”

Click the “start” menu presented at the bottom-left corner of the window

Point the mouse over the “All Programs” item Point the mouse over the “Accessories” item of the sub-menu Click on the “Notepad” application of the sub-menu Click on the “File” from the menu bar of the notepad Using the “Save in” drop down list, navigate to the directory/folder

where the text file is to be saved or created Give a name to the newly created text file by entering a name in the

“File name” box. Click “Save” to save the file By default the extension of the file is “.txt” Press F2 to rename the file name

Page 4: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Using command promt to access the folders Click the “start” menu presented at the bottom-left corner of the

window Point the mouse over the “All Programs” item Point the mouse over the “Accessories” item of the sub-menu Click on the “Command Promt” to open the command promt The above three steps can be done by clicking “Run…” from the start”

menu and entering “cmd” in the text box. Suppose we want to access the “Program Files” folder of the C

directory. We can do so by entering as follows:

cd\ cd Program Files (going into the “Program Files” folder) dir (viewing the content of the folder)

Now, using the above procedure go to the folder that you created

Page 5: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

First Program: Hello World Open a notepad to write a java program that will display “Hello World” as

output. The program is as follows:

class myProgram {public static void main (String args[]){ System.out.println(“Hello world.”);}

} The “bin” directory of java is located at C:\Program Files\Java\jdk1.5.0_12\

bin Save the file into the above directory by giving the file name same as the

class name of the java program that we wrote Here, the file name will be “myProgram.java”. Note that the file extension

is java. To make the file as a java file, choose “All files” from the Save as Types

drop down box at the time of saving. Failure to do will make the file as a normal text file with .txt extension.

Page 6: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Compile and run the program The “bin” directory of java is located at C:\Program Files\Java\

jdk1.5.0_12\bin Go to the above directory by using command promt by using the

following steps: Open command promt and enter the following commands cd\ cd Program Files\Java\jdk1.5.0_12\bin

To compile the java program enter the following command javac myProgram.java

To run the java program enter the following command java myProgram

Page 7: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Explanation of the source codeclass myProgram {

public static void main (String args[]){ System.out.println(“Hello world.”);}

} Java program structured as a set of classes All Java code must be part of a “class” All classes must have a name. Here, our class name is “myProgram” Beginning of a class is identified by a opening brace { Ending of a class is identified by a closing brace } In a class there will be two things

attributes (store some information) methods (perform some specific task)

Page 8: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Explanation of the source codeclass myProgram {

public static void main (String args[]){ System.out.println(“Hello world.”);}

} Here we have only one method The name of the method is “main” “main” is a special name. All java program contains a method with the name “main” When any java program is run, it starts from the “main” method Body of each method contains set of statements Each statement is terminated by semi-solon (;) Here we have only one statement which prints the information between

the quotes.

Page 9: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Taking input from user Write a java program that will ask user to enter his/her name and age.

The purpose of the program is to display the user name and the square of his/her age.

Before starting writing the source code we always try to determine few things What would be the input(s) to the program? What would be the output(s) to the program? How would we calculate the output?

Here, the input is a string (for containing the name) and an integer (for containing the age).

Outputs are string (that contains the name) and integer (that contains the square of the age).

The output of age will be calculated by using the “pow” method of the “Math” class.

Page 10: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Source code with explanationimport java.io.*;import java.util.*;

class myProgram {public static void main(String[] args) throws IOException{

String name;int age;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter your name: ");

name = br.readLine(); System.out.println("Your name is " + name);

System.out.print("Enter your age: ");age = Integer.parseInt(br.readLine());

System.out.println(Math.pow(age,2));}

}

Page 11: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Source code with explanation

throws IOException: An exception that is thrown when I/O error encountered.

public static void main(String[] args) throws IOException

InputStreamReader works as a bridge from byte stream to character stream

It reads bytes and decodes into characters BufferedReader reads text from character input stream

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Page 12: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Alernative sotution Add the following codes just before the last brace } of the Keyboard.java file

public static void main(String[] args){

String name;int age;

Keyboard kb = new Keyboard();

System.out.print("Enter your name: ");

name = kb.readString(); System.out.println("Your name is " + name);

System.out.print("Enter your age: ");age = kb.readInt();

System.out.println(Math.pow(age,2)); // add [import java.util.8;] before the class definition}

}

Page 13: CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Email: tghos009@uottawa.catghos009@uottawa.ca

Assignment Modify or add codes to the Keyboard.java file to do the following

assignments. Take the temperature of a day from the user in degree Celsius and

convert it into degree Fahrenheit by using the formula F = 9/5 * C + 32

Given the value of r and h, find the volume of a cylinder by using the formula V = pi * r * r * h (the value of pi can be obtained by using the Math class)

Due before 8 am on September 28, 2007