C++Spring#Break#Packet## 11# The Java Programming … · Hybrid language—itʼs possible to program in either a C-like ... Java class libraries ! ... Object-oriented analysis and
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
C++ Spring Break Packet 11 The Java Programming Language
! Programmers write instructions in various programming languages, some directly understandable by computers and others requiring intermediate translation steps.
! Three general language types: ! Machine languages ! Assembly languages ! High-level languages
! Any computer can directly understand only its own machine language. ! This is the computer’s “natural language,” defined by its
hardware design. ! Generally consist of strings of numbers (ultimately reduced to
1s and 0s) that instruct computers to perform their most elementary operations one at a time. ! Machine dependent—a particular ma-chine language can be
! Java programs consist of pieces called classes. ! Classes include methods that perform tasks and return
information when the tasks complete. ! Java class libraries ! Rich collections of existing classes ! Also known as the Java APIs (Application Programming Interfaces)
! Two aspects to learning the Java “world.” ! The Java language it-self ! The classes in the extensive Java class libraries
! Download the Java API documentation ! java.sun.com/javase/downloads/ ! Scroll down to the Additional Resources section and click the
Download button to the right of Java SE 6 Documentation.
! Programming tips ! Software Engineering Observations—explain concepts that
affect and improve the overall architecture and quality of software systems. ! Good Programming Practices—help you write programs that
are clearer, more understandable, more maintainable and easier to test and debug—i.e., remove programming errors. ! Common Programming Errors—discuss problems to watch out
! Phase 1 consists of editing a file with an editor program (normally known simply as an editor). ! Type a Java program (source code) using the editor ! Make any necessary corrections ! Save the program ! A file name ending with the .java extension indicates that the file
contains Java source code. ! Linux editors: vi and emacs. ! Windows editors: Notepad, EditPlus (www.editplus.com), TextPad (www.textpad.com) and jEdit (www.jedit.org).
! Phase 3 ! The JVM places the program in memory to execute it ! This is known as loading. ! Class loader takes the .class files containing the program’s
bytecodes and transfers them to primary memory. ! Also loads any of the .class files provided by Java that your
program uses. ! The .class files can be loaded from a disk on your system or
! Object-oriented design (OOD) ! Models software in terms similar to those that people use to
describe real-world objects. ! Class relationships ! Inheritance relationships ! Models communication between objects (via messages). ! Encapsulates attributes and operations (behaviors) into objects. ! Information hiding ! Objects may know how to communicate with one another across
well-defined interfaces, but normally they are not allowed to know how other objects are implemented.
! Object oriented languages ! Programming in such a language, called object-oriented
programming (OOP) ! Allows you to implement an object-oriented design as a working
system. ! Java is object oriented.
! Focus on creating classes. ! Each contains fields and the set of methods that manipulate the fields
and provide services to clients (i.e., other classes that use the class). ! Programmers use existing classes as the building blocks for
constructing new classes. ! Classes are to objects as blueprints are to houses. ! Associations—relationships between classes. ! Packaging software as classes facilitates reuse.
Successful completion of this assignment will include the following: Have a tab separating this from other sections. Summarize each key point in the video tutorials. Give an example of the implementation of each key point. (In some cases, you will need to provide a screenshot of proof of implementation.) Find a separate web reference for each of the key points listed above. Wikis will not be allowed. Provide a reference list in APA format.
What is Java? http://www.youtube.com/watch?v=hNvkR1LiV0w
Installing the JDK and IDE http://www.youtube.com/watch?v=EhH02lqJ4tM and http://www.youtube.com/watch?v=-‐CSyFuBpuZQ
Running a Java Program http://www.youtube.com/watch?v=_7rD_qkmlXo and http://www.youtube.com/watch?v=BhCU8UYJHYI
C++ Spring Break Packet 22
Practice Problems
Use the scantron provided in class. Q1: End-of-line comments that should be ignored by the compiler are denoted using a. a. Two forward slashes ( // ). b. b. Three forward slashes ( /// ). c. c. A slash and a star ( /* ). d. d. A slash and two stars ( /** ). Q2: Which of the following is not a valid Java identifier? a. a. my Value b. b. $_AAA1 c. c. width d. d. m_x Q3: Which of the following cannot cause a syntax error to be reported by the Java compiler? a. a. Mismatched {} b. b. Missing */ in a comment that begins with /* c. c. Missing ; d. d. An extra blank line. Q4: Which of the following does not contain a syntax error? a. a. System.out.println( 'Hello world!' ): b. b. System.out.println( "Hello world!" ); c. c. System.out.println( "Hello world!" ); d. d. System.out.println( Hello world! ); Q5: Which command compiles the Java source code file Welcome.java? a. a. cd Welcome.java b. b. javac Welcome.java c. c. java Welcome.java d. d. compile Welcome.java Q6: Which command executes the Java class file Welcome.class? a. a. javac Welcome.class b. b. java Welcome.class c. c. java Welcome d. d. run Welcome.class Q7: Which is the output of the following statements? System.out.print( "Hello "); System.out.println( "World" ); a. a. Hello World b. b. HelloWorld c. c. Hello World d. d. World Hello Q8: Which of the following is the escape character? a. a. * b. b. \ c. c. \n d. d. "
C++ Spring Break Packet 23 Q9: Which of the following statements will print a single line containing "hello there"? a. a. System.out.println( "hello" ); System.out.println( " there" ); b. b. System.out.println( "hello" , " there" ); c. c. System.out.println( "hello" ); System.out.print( " there" ); d. d. System.out.print( "hello" ); System.out.println( " there" ); Q10: Which of the following escape sequences represents a carriage return? a. \n. b. \r. c. \cr. d. \c. Q11: Which of the following statements would display the phase Java is fun? a. a. System.out.println( "hellois fun\rJava " ); b. b. System.out.println( 'Java is fun' ); c. c. System.out.println( "\"Java is fun\"" ); d. d. System.out.println( Java is fun ); Q12: All import declarations must be placed a. inside the class declaration’s body. b. before the class declaration. c. after the class declaration. d. all of the above will work. Q13: Which of the following is a variable declaration statement? a. a. int total; b. b. import java.util.Scanner; c. c. public static void main( String args[] ) d. d. // first string entered by user Q14: Which of the following statements does not alter a memory location? a. a. int a; b. b. number = 12; c. c. y = y + 2; d. d. width = Integer.parseInt(input); Q15: What will be output after the following Java statements have been executed?
int a, b, c, d; a = 4; b = 12; c = 37; d = 51; if ( a < b ) System.out.println( "a < b" ); if ( a > b ) System.out.println( "a > b" );
if ( d <= c ) System.out.println( "d <= c" ); if ( c != d ) System.out.println( "c != d" ); a. a. a < b c != d b. b. a < b d <= c c != d c. c. a > b c != d d. d. a < b c < d a != b