Upload
derrick-goodman
View
227
Download
0
Tags:
Embed Size (px)
Citation preview
Welcome
Smith
Door
AP Computer Science
Computer Science at West• Robotics Programming (RobotC) (One Semester)• VEX Robotics/Engineering
• Computer Science I and II (Pascal) (One semester each)• AP Computer Science (Java) (One year)• Prepares students for the ‘A’ level exam
• Advanced Computer Projects (One semester, can be taken repeatedly) • Developing original software
• Robotics Projects• VEX Robotics Challenge (Mainly first semester)• Spring tech conference (Second Semester)/FTC/VEX If we make
the finals.• Introduction to Engineering Design• Design process• 2 D and 3D design by hand• 3D design on computer (Inventor)
Class Overview: First Semester• Basic I/O• Math in Java• Decision construct• For loop• Java Docs and Strings• While• Do..while*• Random• Arrays of Primitives• Midterm
• More Arrays• Sorting• Intro to Objects• Inheritance• Intro to Gridworld• More OOP• ArrayList Class• Intro to OSI Networking
model• Review and Final
Grading Policy
• Grading Scale• A .. 90%+• B .. 80 – <90%• C .. 70 – <80%• D .. 60 – <70%• F .. Below 60%
• Assignments• Programs 10 points• Quizzes: 25 points• Tests: 100 Points• Projects: 20 to 100 points• Final: 200 points or 20% of
your grade, whichever is less.
Getting Started: Create Shortcuts
• Log onto the network• Your password is your Student ID# followed by a period.
• Open MyDocuments• Make a new folder for Java
• Keep the Java Folder open • Navigate to west on 'dtcfsc04\coursework(R:)‘• Double click on SMITH_GREG folder• Double click on the APJava folder• Drag the R:\Smith_Greg\APJava folder from the address bar and drop it into your Java
folder.• Open BlueJ
Getting Started in BlueJ• Open BlueJ• Make a New Project
(Folder)• Create a new class
(file)
Start with a capital letter.
Welcome1
Fill in the Class
Delete info between the {} after the public
class Welcome1
Enter your name, brief description of the program, the
date and version 1.0.
First Program Enter the following program in BlueJ.
Compiling the Program
Click Compile
Check for errors.
Running the ProgramRight click on
the class.Select void
main()
Click OK
Output Screen
Turn in the .java file
Rename the java file to YourNameWelcome1 and
turn in to the turn in folder.
Include your name in the file name
public class Welcome1 • Begins the class definition of class Welcome1• Every program must include the definition of at least one class.• Class names begin with a capital letter• The class name must match the file name (Welcome1.java)• This is case sensitive• The file must have a .java extension (BlueJ should do this for you.)• Identifier rules• //Series of characters (letters, numbers, digit, _, $• //Must not start with a number• //Cannot be a reserved word (purple in editor)• Must describe what it is storing!
• Java IS Case sensitive
Breaking down the code.
Get out your notes.
{• Same as BEGIN in Pascal• Marks the beginning of the main body of the class.• Needs to match up with a }• Indent between the {}
public static void main(String args[])
• This is the starting point of every java application• (Main body of the program)
• () after main indicate it is a method (Like a procedure)• Java classes usually contain more than one method• Exactly one of these methods must be called main and
defined as above for the program to run.• Methods perform tasks and return information. Kind of
like functions• void indicates that it will return nothing.• String args[] is required for the main's definition. More
details later.
System.out.println("Welcome to Java programming!");
• System.out is the standard output object for showing info in the command window
• The stuff inside () are the arguments• System.out.println(); Displays the arguments and returns to
the next line• Like writeln in Pascal• "Wel..." This is a String of characters, message or string literal.
Comments and Common Errors• When successfully compiled it creates a Class file for
Welcome1 called Welcome1.class• This file contains the byte codes that represent this
application
• Some common errors• "bad command or file name", "javac: command not found",
""'javac' is not recognized as an internal or external command, operable program or batch file"• Try: The system's path environment was not set properly. Check
java.sun.com/j2se/5.0/install.html• “Public class ClassName must be defined in file called
ClassName.java"• Try: Checking that the file name exactly matches the class name.
Review// Mr. Smith// First Program// Today’s datepublic class Welcome1 {
//main method begins execution of Java applicationpublic static void main(String [] args) { //The start of the method
System.out.println("Welcome to Java programming!");}//End of method main
} //End of class Welcome1
More on System.outpublic class Welcome1 {
//main method begins execution of Java applicationpublic static void main(String [] args) { //The start of the method
System.out.print("Welcome to "); //Stays on the same lineSystem.out.println(“Java programming”);
}//End of method main} //End of class Welcome1
Showing Multiple lines public class Welcome1 {
//main method begins execution of Java applicationpublic static void main(String [] args) { //The start of the method
System.out.println(“Welcome \nto \nJava \nprogramming”);}//End of method main
} //End of class Welcome1
Escape Sequences
\ is an ‘escape’ characterSome Common Escape Sequences\n for new line\t horizontal tab\r Carriage return, but on the same line\\ used to print the \character\” Used to display “
Displaying Formatted Text (printf)
public class Welcome1 {
public static void main(String [] args) { //The start of the method System.out.printf(“%s\n%s\n”,"Welcome “,” to Java programming!");
}//End of method main} //End of class Welcome1
System.out.printf(Format String, Comma separated data to display)
Format String
Can contain fixed text, escape sequences and format specifiers
Format Specifiers: Begin with %
%s is a place for a string.
System.out.printf( “format-string” [, arg1, arg2, … ] ); • Format String:
• Composed of literals (String characters) and format specifiers (%d, %-,.2f
• Arguments are required only if there are format specifiers in the format string. Format specifiers include: flags, width, precision, and conversion characters in the following sequence:
• % [flags] [width] [.precision] conversion-character ( square brackets denote optional parameters )
Flags• % [flags] [width] [.precision] conversion-character ( square
brackets denote optional parameters )
• Flags: - : left-justify ( default is to right-justify ) + : output a plus ( + ) or minus ( - ) sign for a numerical value 0 : forces numerical values to be zero-padded ( default is blank padding ) , : comma grouping separator (for numbers >= 1000) : space will display a minus sign if the number is negative or a space if it is positive
Width• % [flags] [width] [.precision] conversion-character ( square
brackets denote optional parameters )
• Specifies the field width for outputting the argument and represents the minimum number of characters to be written to the output.
• Include space for expected commas and a decimal point in the determination of the width for numerical values.
Precision• % [flags] [width] [.precision] conversion-character ( square
brackets denote optional parameters )
• Used to restrict the output depending on the conversion. • It specifies the number of digits of precision when outputting
floating-point values . Numbers are rounded to the specified precision.
Common Conversion Characters
• % [flags] [width] [.precision] conversion-character ( square brackets denote optional parameters )
• • d : decimal integer [byte, short, int, long] • f : floating-point number [float, double] • c : character Capital C will uppercase the letter • s : String Capital S will uppercase all the letters in the string• n : newline Platform specific newline character- use %n
instead of \n for greater compatibility
Examples• % [flags] [width] [.precision] conversion-character ( square
brackets denote optional parameters )
• System.out.printf("Total is: $%,.2f%n", dblTotal);
• System.out.printf("Total: %-10.2f: ", dblTotal);
• System.out.printf("% 4d", intValue);
More Examples:Controlling integer width with printf
printf("%3d", 0); 0
printf("%3d", 123456789); 123456789
printf("%3d", -10); -10
printf("%3d", -123456789); -123456789
The %3d specifier means a minimum width of three spaces, which, by default, will be right-justified
Left-justifying printf integer output
printf("%-3d", 0); 0
printf("%-3d", 123456789); 123456789
printf("%-3d", -10); -10
printf("%-3d", -123456789); -123456789
To left-justify integer output with printf, just add a minus sign (-) after the % symbol, like this:
The printf zero-fill option
printf("%03d", 0); 000
printf("%03d", 1); 001
printf("%03d", 123456789); 123456789
printf("%03d", -10); -10
printf("%03d", -123456789); -123456789
To zero-fill your printf integer output, just add a zero (0) after the % symbol, like this:
printf integer formatting
Description Code Result
At least five wide printf("'%5d'", 10); ' 10'
At least five-wide, left-justified printf("'%-5d'", 10); '10 '
At least five-wide, zero-filled printf("'%05d'", 10); '00010'
At least five-wide, with a plus sign printf("'%+5d'", 10); ' +10'
Five-wide, plus sign, left-justified printf("'%-+5d'", 10); '+10 '
As a summary of printf integer formatting, here’s a little collection of integer formatting examples. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.
printf - floating point numbers
Description Code Result
Print one position after the decimal printf("'%.1f'", 10.3456); '10.3'
Two positions after the decimal printf("'%.2f'", 10.3456); '10.35'
Eight-wide, two positions after the decimal printf("'%8.2f'", 10.3456); ' 10.35'
Eight-wide, four positions after the decimal printf("'%8.4f'", 10.3456); ' 10.3456'
Eight-wide, two positions after the decimal, zero-filled printf("'%08.2f'", 10.3456); '00010.35'
Eight-wide, two positions after the decimal, left-justified
printf("'%-8.2f'", 10.3456); '10.35 '
Printing a much larger number with that same format
printf("'%-8.2f'", 101234567.3456); '101234567.35'
printf string formatting
Description Code Result
A simple string printf("'%s'", "Hello"); 'Hello'
A string with a minimum length printf("'%10s'", "Hello"); ' Hello'
Minimum length, left-justified printf("'%-10s'", "Hello"); 'Hello '
Quick Review • Starting a project in BlueJ• Open BlueJ• Select your workspace (Folder)• Project->New Project• Click on ‘Class’ to create a class• Double click on icon to edit the code
• Parts of your program• public class FormattedPrint • {• public static void main(String [] args) • {//The start of the method• System.out.printf("%s\n%s\n","Welcome "," to Java programming!");• }//End of method main• }
• Time to get started.
Your first programs• Poem/Song: (YourNamePoem-Song)• Create or modify a poem or song to display from your Java
Program.• You will need to incorporate at least one printf.
• Check: Using printf (YourNameCheck)• No input• Display information on the screen for a check with your generous
donation to the West Salem Robotics Club.• You will need to incorporate at least one printf.