java Test marcel 123 true public static void main (String [] args)

Download Report

Transcript java Test marcel 123 true public static void main (String [] args)

ITI 1221, Introduction to Computing - II
Lab-1
Dewan Tanvir Ahmed
University of Ottawa
Contact Information
•
•
•
Email: [email protected]
Office hour: Mon 1400~1600
Office location:
– Discover Lab
– Room 5040
– SITE
•
Website: http://www.site.uottawa.ca/~dahmed
General Instructions for Assignments
•
Assignment URL:
– http://www.site.uottawa.ca/~turcotte/teaching/iti-1221/assignments/
•
Notes
– Do the assignments individually or a team of two
– Submission by WebCT
• Don’t forget to hit Submit button
– Indent your code properly
– Late assignments will not be accepted; they will receive a grade of 0;
– It is not acceptable to hand in code that does not compile
• Maximal mark in this case be 30%
– A Tour:
• http://www.site.uottawa.ca/~turcotte/teaching/iti-1221/assignments/directives.html
General
•
•
•
Late assignments will not be accepted; they will receive a grade of 0;
Assignments must be done in teams of two or individually;
Assignments are due on Sundays by 23:00 (11 p.m.)
Identify yourself
•
•
README should be a text file and not a Word document
All this should appear in all of the following places:
– at the beginning of each program/class (in comments)
– at the beginning of each program's output
Example of a class header:
// Author: Linus Torvald
// Student number: 111223
// Course: ITI 1221-A
// Instructor: Marcel TURCOTTE
// Tutorial: DCG-2
// Teaching Assistant: Dewan Tanvir Ahmed
// Assignment: 4
// Question: 2
Restrictions and Marking scheme
•
Restrictions
– Everything you hand in
• must compile and run using the J2SE 1.4.2 system on SITE's PCs;
– The maximum grade for an assignment that does not compile is 30%;
– You may only use features and library methods of Java
• that have been presented in class or tutorials at the time an assignment is due.
•
Marking Scheme
– based on the code's correctness and quality
– Each part of your solution (e.g. each method)
• assessed individually
• relative to the conditions and requirements stated in the questions
Handing an assignment
•
All assignments must submitted electronically using your virtual campus
account.
– follow the guidelines for naming the files (specific to each assignment);
– virtual campus:
•
virtualcampus.uottawa.ca
– For teams of two students
•
•
•
•
•
Only one of the two students submits a copy of the assignment
Clearly identify your partner and yourself.
Assignments that do not conform to the above procedure will not be
marked;
Never upload the the byte-code files (files ending with the suffix .class);
All the assignments are systematically re-compiled.
Programming Standards
•
Follow the standard code conventions for Java
– http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
•
Identifiers
– class and interface names begin with an upper case letter;
– method and variable names begin with a lower case letter
•
except for the constructors
– Several words may construct a name
•
•
•
•
not separated by underscores
and the second and subsequent words should start with an upper case letter
Class: StackArray,
Variable: maxValueAtrribute
Programming Standards (cont..)
– names of constants
•
•
Single word: PI
Multiple words: MAX_VALUE
– a method name should begin with a verb
•
computeMaxmimum not just maximum
– a method whose primary purpose is to access an instance variable
•
•
•
getAttribute
setAttribute
isAttributeEqualTo(value)
– Emphasis relation
Variables
•
•
•
•
Only declare variables that are actually used
Do not allocate memory unless necessarily
Do not use the same variable for different purposes
Local variables should not be confused with instance variables:
– declare each variable in its appropriate place;
•
Every variable should have a comment describing its purpose.
– Be brief.
– This is called a data dictionary
– it should be placed
• at the beginning of every method,
• either before or beside the declarations.
Program Structure
•
Write code with good structure
– Introduce your own abstract classes and/or interfaces and
– decompose your algorithms/methods into meaningful sub-algorithms/methods
•
Each method, including "main“, should be preceded by comments
–
–
–
–
•
describing its purpose and
the algorithm it uses
if the algorithm is very simple, it needn't be described
should be BRIEF.
Your program must be indented in a systematic way that reflects the nesting
of its statements.
Input/Output
•
Output should be
– nicely spaced and
– easy to understand.
•
Every value in your output
– must print a short message explaining the meaning of the value
•
Before your program reads values from the keyboard
– it must print a prompt describing what the user is required to enter
Lexical Scope
•
Definition:
– The scope of an identifier is fixed at compile time to some region in the source
code containing the identifier’s declaration.
– This identifier is only accessible within that region.
•
Examples:
More Examples
class Scope{
static int i;
public static void main(String[ ] argv){
System.out.println("Value of i: " + i);
}
}
class Scope{
static int i;
public static void main(String[ ] argv){
int i;
i = 50;
System.out.println("Value of i: " + i);
}
}
class Scope{
static void increment(){
i++;
}
public static void main(String[ ] argv){
int i;
i = 10;
increment();
System.out.println("Value of i: " + i);
}
}
class Scope{
public static void main(String[ ] argv){
int i;
i = 10;
if(i<20){
int save = i*3;
}
System.out.println("Value of save: " + save);
}
}
Data Type Conversion
•
Primitive data types
– int
– long
– double
class DataType{
public static void main(String[] agrv){
System.out.println("An \"int\" can hold a maximum of " + Integer.MAX_VALUE);
System.out.println("An \"long\" can hold a maximum of " + Long.MAX_VALUE);
System.out.println("An \"double\" can hold a maximum of " + Double.MAX_VALUE);
}
}
An "int" can hold a maximum of 2147483647
An "long" can hold a maximum of 9223372036854775807
An "double" can hold a maximum of
1.7976931348623157E308
Type Conversion
•
Implicit
– The ability of some compilers to automatically insert type conversion functions where an
expression of one type is used in a context where another type is expected.
•
Explicit
– Type casting
– Narrowing and the possibility of data loss
Type Conversion
class DataType{
public static void main(String[] agrv){
int x, z;
double y;
x = 7;
y = 8.3;
System.out.println("Value
z = (int)(x*y);
System.out.println("Value
z = (int) ((double) x * y);
System.out.println("Value
z = x*(int)y;
System.out.println("Value
of the expresion <x*y>: " + x*y);
of z: " + z);
58
of z: " + z);
58
of z: " + z);
56
y = x*y;
System.out.println("Value of updated y: " + y);
}
}
58.1
58.1
Wrapper Classes
•
Useful to convert data read from the keyboard
class WrapperClass{
public static void main(String[] argv)
{
int i;
double d;
i = Integer.parseInt("78");
d = Double.parseDouble("45.0215");
System.out.println("Integer has a value of " + i);
System.out.println("Double has a value of " + d);
}
}
Modulo Arithmetic
•
•
•
Division ( / )
Reminder/Modulus ( % )
Example:
– 11 / 4 (2)
– 11 % 4 (3)
•
Brain storming
– 14/3 + 17%5 ?
– (your student number % 3) ?
•
Just make it interesting.
– Reverse a positive number ?
– Example:
• Input: 1234
• Output: 4321
– Can you do it?
Modulo Arithmetic (Cont..)
class Modulo{
int n;
Modulo(int n){
this.n = n;
}
int reverseNumber(){
int rem, rev;
rev = 0;
while(n!=0){
rem = n % 10;
n = n / 10;
rev = rev*10 + rem;
}
return rev;
}
public static void main( String [] args){
Modulo m = new Modulo(1234);
System.out.println("Reverse of number: " + m.n + " ....." + m.reverseNumber());
}
}
Application Program Interface (API)
•
•
•
A tour: http://java.sun.com/j2se/1.4/docs/api/overview-summary.html
Information is presented in a nice hierarchy
Object
– String
– Number
• Integer
• Double
•
Example of overriding
class Override{
public static void main(String[] argv) {
Override or = new Override();
System.out.println("What is this? " + or.toString());
}
}
class Override{
int x;
public String toString(){
return "Overrirded: " + x;
}
public static void main(String[] argv)
{
Override or = new Override();
or.x = 15;
System.out.println("What is this? " + or.toString());
}
}
Compile and Run
•
•
•
•
Write a program that will print our name and student Id.
Compile it
Run it
Group formation among students
•
Use DrJava
Command Line
•
•
•
•
It is a way to pass information to the program
Say, java Test marcel 123 true
public static void main (String [] args)
What is args?
– Reference that designates an array of String
– Each element of this array is also a reference
•
Write a program that will take 3 arguments from command
prompt and print their summation
•
Write a class that has a main method that displays the arguments
on command line
Programming Editor
•
Good programming Editor
– Saves time
– Takes care indentation
– Color coding helps finding the syntax errors.
•
Let’s go and play with Editor
Assignment 0
•
Objective
– Editing, compiling and running a simple Java program
– Raising awareness about the university policies for academic frauds
– Submitting an assignment using virtual campus online submission system.
•
A tour:
– http://www.site.uottawa.ca/~turcotte/teaching/iti-1221/assignments/
–
–
–
–
–
–
Editing, compiling and running…Java program
Syllabus
Academic fraud
Online submission
Directives
Files
• README
• Ao.java
Assignment 0 (Cont..)
•
public static void displayStudentInfo( )
– Displays the student name, id and laboratory section
•
public static String[] findAndReplace( String[] in, String[] what, String[] with )
text[ 0 ] = new String( "I" );
text[ 1 ] = new String( "have" );
text[ 2 ] = new String( "never" );
text[ 3 ] = new String( "let" );
text[ 4 ] = new String( "my" );
text[ 5 ] = new String( "schooling" );
text[ 6 ] = new String( "interfere" );
text[ 7 ] = new String( "with" );
text[ 8 ] = new String( "my" );
text[ 9 ] = new String( "education" );
query[ 0 ] = text[ 0 ];
query[ 1 ] = text[ 1 ];
query[ 2 ] = text[ 4 ];
replacement[ 0 ] = new String( "You" );
replacement[ 1 ] = new String( "should" );
replacement[ 2 ] = new String( "your" );
String[ ] result;
result = findAndReplace( text, query, replacement );
You should never let your
your schooling interfere with your education
Happy Programming!!!
To whom Java is new!
•
The URL for ITI 1220 is
– http://www.site.uottawa.ca/~awilliam/iti1220
•
If you need help on setting up Java on home computers page:
– http://www.site.uottawa.ca/~awilliam/iti1220/WorkAtHome.html
•
It is important to set the PATH as described at the following page
– http://java.sun.com/j2se/1.5.0/install-windows.html
•
Course website:
– http://www.site.uottawa.ca/~turcotte/teaching/iti-1221/
•
Hands on practice