chapter 2 - first java programs

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/97

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

98 Terms

1
New cards

Java

modern object-oriented programming language

2
New cards

features of Java

secure, robust, and portable

3
New cards

how is Java secure?

Enables the construction of virus-free, tamper-free systems

4
New cards

how is Java robust?

Supports the development of programs that do not overwrite memory

5
New cards

how is Java portable?

Yields programs that can be run on different types of computers without change

6
New cards

what are the features of Java that once made it the fastest-growing language of all time?

1) it's a modern object-oriented programming language. Java was inspired by combining the good features of C++ and Smalltalk and removing the less desirable features. 2) it's secure, robust, and portable. 3) Java supports the use of advanced programming concepts, ex. threads. 4) Java bears a superficial resemblance to C++, but it's easier to use & learn, less error-prone, and better suited to the internet.

7
New cards

thread

a process that can run concurrently with other processes, ex. in a simple Java application, one thread transfers an image from one machine to another across a network, while the other thread simultaneously interacts with the user

8
New cards

C++

the world's most popular industrial-strength programming language

9
New cards

why does Java run more slowly than most modern programming languages?

it is interpreted

10
New cards

interprative programming language

generally interpreted, without compiling a program into machine instructions. It is one where the instructions are not directly executed by the target machine, but instead, read and executed by some other program. (in Java it's the JVM)

11
New cards

what is a portable program?

a program that can be carried out (compiled/interpreted & executed) across multiple devices without reconstruction or hassle

12
New cards

Describe two features of Java that make it a better language than C++.

Java is easier to use & understand, and is better suited for the internet

13
New cards

What is a thread? Describe how threads might be used in a program.

a thread is a process in a program which acts concurrently with other processes. for ex. in a simple Java program, one thread transfers an image from one machine to another while another thread interacts with the user.

14
New cards

(Java) byte code

machine language for an imaginary Java computer, called pseudomachine language since it's not an actual machine language (language understandable to machines, ex. bits)

15
New cards

Java Virtual Machine (JVM)

an interpreter (program that behaves like a computer) that runs Java byte code

16
New cards

interpreter

program that behaves like a computer, runs program slower than an actual computer (disadvantage), but any computer can run it (advantage)

17
New cards

Just-in-time compilation (JIT)

a technique in which byte code is translated into machine language when first encountered so that the next time the instruction is encountered, it is executed as fast machine code rather than being interpreted as slow byte code (works "just in time")

18
New cards

applets

small programs already translated into byte code, has a wide range in performing many applications (not only in Java, includes other languages & types - but MOST of the time refers to JAVA programs running in WEB BROWSERS)

19
New cards

what are consequences of Java programs running in virtual machines?

their capabilities are limited, so programs can't infect computers with viruses, erase files from someone's hard drive, or steal sensitive data & sending it to competitors. (hackers have succeeded in the past but security weaknesses are repaired as soon as they're known).

20
New cards

what does JVM stand for?

Java Virtual Machine

21
New cards

What is byte code? Describe how the JVM uses byte code.

byte code in Java is a pseudomachine language in which the compiled program is translated into byte code on the JVM, then the program is executed

22
New cards

What is an applet? Describe how applets are used.

an applet is a small program of byte code, it typically refers to small programs of Java byte code that can run in web browsers, however applets are not limited to Java or web browsers. they have a wide range of purposes, from showing animated characters, graphics, or a continuous stream of the stock market.

23
New cards

user interface

The visual elements of an program through which a user controls or communications the application. Often abbreviated UI. (kinda like appearance, look, feel, vibe, or interaction with the program)

24
New cards

graphical user interface (GUI)

familiar to all PC users, familiar & comfortable

25
New cards

terminal I/O user interface

A user interface that allows the user to enter input from a keyboard and view output as text in a window, less common & less comfortable.

26
New cards

3 reasons for using terminal I/O

1) easier to implement than GUI (exception: Visual BASIC). 2) there are programming situations that require a terminal I/O than a GUI. 3) terminal-oriented programs are similar in structure to programs that process files of sequentially organized data.

27
New cards

program

a sequence of instructions for a computer, does nothing until executed by computer

28
New cards

source code

plain text computer program written in a programming language, like a bulk of instructions for a program written in code

29
New cards

System.out.println("Hello World");

prints out the phrase "Hello World"

30
New cards

System.out

the name of an object that knows how to display or print characters in a terminal window

31
New cards

println

is the name of the message being sent to the System.out object

32
New cards

"Hello World" or other strings enclosed in quotation marks

contain the content/characters to be printed

33
New cards

semicolon (;)

marks the end of each statement or sentence in the program

34
New cards

how does an object-oriented program accomplish its tasks?

by sending messages to objects

35
New cards

explain this example program: System.out.println("Hello World");

a System.out object responds to a println message by printing a string of characters (in this case, "Hello World") in the terminal window

36
New cards

parameter

the string characters that appears between the parentheses following the message. some messages require several of these separated by commas, and other messages might not have them.

37
New cards

ln (in println)

stands for "line" & indicates that the System.out object should advance to the beginning of the next line after printing a string

38
New cards

structure of sending messages to objects

.();
39
New cards

method selector operator

period (.) between object's name & message, mechanisms or syntax used to identify and/or invoke specific methods or functions within a programming context. (The period in System.out is NOT this).

40
New cards

Give a short definition of "program.

a set of code/steps to be executed by a computer

41
New cards

What is the effect of the message println?

prints the statement and leaves an empty line after that, the cursor goes to the next empty line - NOT right after the last printed character

42
New cards

Describe how to use the System.out object.

you can use that object for outputting data to the console or other standard output streams

43
New cards

Write a sequence of statements to display your name, address, and phone number in the terminal window

44
New cards

steps to enter Java code into a computer and run it

edit, compile, execute

45
New cards

edit

In this first step, the programmer uses a word processor or editor to enter the source code into the computer and save it in a text file. The name of the text file must match the name of the program with the extension .java added, as in HelloWorld.java.

46
New cards

compile

In this second step, the programmer invokes the Java language compiler to translate the source code into Java byte code. In this example, the compiler translates source code in the file HelloWorld.java to byte code in the file HelloWorld.class. The extension for a byte code file is always .class.

47
New cards

execute

In this third step, the programmer instructs the JVM to load the byte code into memory and execute it. At this point the user and the program can interact, with the user entering data and the program displaying instructions and results.

48
New cards

development environment

a workspace where software is created, tested, and debugged. details involved in editing, compiling, and running a program vary depending on which one of these is used.

49
New cards

common development environments available to Java programmers

UNIX, Linux, Notepad (with command-line activation), BlueJ, Eclipse, JGrasp

50
New cards

UNIX

using a standard text editor with command-line activation of the compiler and the JVM. available on any Macintosh computer that runs MacOS X. free and may require you to download and install the Java software development kit (SDK).

51
New cards

Linux

(Java) development environment using a standard text editor with command-line activation of the compiler and the JVM

52
New cards

Notepad (Microsoft Windows)

used as an editor, with command-line activation of the compiler and the JVM from inside a command or DOS window. it's a DOS development environment

53
New cards

DOS development environment

collection of tools and resources used to create software applications specifically for the Disk Operating System (DOS). used to have the most widespread use.

54
New cards

integrated development environment (IDE)

may cost money, but it has the advantage of combining an editor, a Java compiler, a debugger, and a JVM in a manner intended to increase programmer productivity. take time to master, however, and they can obscure fundamental details of the edit, compile, and run sequence.

55
New cards

Step 1

Use Windows Explorer to create the directory in which you intend to work (for instance, C:\Javafiles). Open a terminal window by selecting Command Prompt (or something similar) on the Start/All Programs/Accessories menu. In the terminal window, use the cd command to move to the working directory.

56
New cards

Step 2

Open the Notepad editor and create the file HelloWorld.java by typing the text as shown in the image. Once Notepad opens, click Yes in the dialog box to create the file and then type in the lines of code for the program.

57
New cards

Step 3

Write the code, save the file and switch back to the terminal window. Compile the program by typing javac HelloWorld.java. The DOS prompt returns when the compilation is complete.

58
New cards

Step 4

Run the program by typing java HelloWorld.

59
New cards

compile-time errors

Mistakes detected by the compiler, also called syntax errors

60
New cards

error in the input

On line 6 of the program, println is misspelled as prinrln.

61
New cards

error message printed in output

When the program is compiled, the compiler prints a list of errors in the terminal window. The error messages refer to line 6 of the source program and says that a symbol cannot be found. More specifically, the symbol for method prinrln within the class java.io.PrintStream cannot be found. In fact, the object System.out is a member of this class, and the program has attempted to send a message that the class does not recognize. A caret symbol (^) points to the location of the error in the code. Error messages indicate where the compiler encountered text it could not translate into byte code.

62
New cards

readability

the quality of being legible or decipherable

63
New cards

why is readability important in a program?

Programs typically have a long life and are usually maintained by many people other than their original authors. They need to be easy to read, comprehend, and edit for other programmers.

64
New cards

what is the main factor affecting a program's readability?

The main factor affecting a program's readability is its layout. Indentation, the inclusion of blank lines and spaces, and other typographical considerations make the difference between an intelligible program and an incomprehensible mess.

65
New cards

does a program's layout, format, or readability matter when executing it?

No, the compiler completely ignores a program's format, provided that there are no line breaks in the middle of words or quoted strings. (The program in the image is completely functional, but it is just unreadable).

66
New cards

hacking

A term whose use goes back to the early days of computing. In its original sense, they are a programmer who exhibits rare problem-solving abilities and commands the respect of other programmers. regarded as an accomplishment along the lines of Olympic gymnastics. Unfortunately, this practice has changed over the years, and the term has acquired darker connotations.

67
New cards

a "hack"

a programming maneuver that exhibits technical brilliance and elegance

68
New cards

hacker ethic

hackers should respect the privacy of others and distribute their software for free

69
New cards

hackers

Programmers who break into computer systems in an unauthorized way, whether their intent is just to impress their peers or to cause actual harm. Also includes students and professionals who lack a disciplined approach to programming.

70
New cards

Name the three steps in writing and running a program.

Edit, compile, execute

71
New cards

What are compile-time errors?

Errors detected by the compiler, also called syntax errors

72
New cards

Find the compile-time errors in statements A and B.

73
New cards

A. System.out.println("Here is an error);

74
New cards

B. System.out.println("Here is another error";

Statement A's string is not closed by a second quotation mark. Statement B's string is not closed by a second parenthesis.

75
New cards

Why is readability a desirable characteristic of a program?

It makes programs easy to read, understand, and edit. This is especially paramount if programs are being shared among other users who are constantly editing the code. It needs to be understandable for other people to look at.

76
New cards

temperature conversion program

reads user inputs and performs computations, demonstrates several important concepts despite brevity & simplicity

77
New cards

How is instantiation done like in general?

SomeClass someObject = new SomeClass(some parameters);

78
New cards

System.in

names a variable in the System class that refers to the keyboard

79
New cards

parameters

used to share information between objects

80
New cards

variables

names that refer to data values or objects.

81
New cards

numeric variable

names a location in RAM in which a number can be stored. The number is usually referred to as the it's value.

82
New cards

type double variables

contain only floating-point numbers

83
New cards

is it required that all variables in Java begin with a lowercase lettter?

no, it is only customary

84
New cards

assignment operator

=

85
New cards

assignment statements

Statements utilizing an assignment operator

86
New cards

*

indicates the multiplication operator

87
New cards

/

indicates the division operator

88
New cards

-

indicates the subtraction operator

89
New cards

+

indicates the addition operator

90
New cards

Java operator precedence

PEMDAS

91
New cards

variables celsius and fahrenheit

hold numbers

92
New cards

variables reader and System.out

hold references to objects

93
New cards

What is a variable in a program and how is it used?

Stores data in a "container" and can be used & reused for complexity

94
New cards

Scanner class

Used for obtaining user input in Java.

95
New cards

Describe the role of the assignment (=) operator in a program.

assigns values/expressions to variables

96
New cards

What is a Scanner object?

A Scanner object receives messages to input data from a human user. For example, when sent the message nextDouble, a Scanner object causes a program to wait for a user to enter the digits of a number. When the user presses the Enter key, the object returns the corresponding floating-point number.

97
New cards

Explain the difference between a variable of type double and a variable of type Scanner.

a double variable tyle holds numerical floating-point values. a scanner variable type holds references to the scanner class & object

98
New cards

Describe the difference between print and println, and give an appropriate example of the use of each.

using print causes the cursor at the end of the last character to appear exactly after the character. using println causes the cursor to appear in the beginning of the next line.