Anda di halaman 1dari 22

Chapter 1 Introduction to Computer Science This has been an introductory hodge-podge chapter.

. It is awkward to jump straight into computer science without any type of introduction. Students arrive at a first computer science course with a wide variety of technology backgrounds. Some students know a little keyboarding and Internet access along with basic word processing skills taught in earlier grades. Other students come to computer science with a sophisticated degree of knowledge that can include a thorough understanding of operating systems and frequently knowledge of one or more program languages as well. The secret of computer information storage and calculation is the binary system. Information is stored in a computer with combinations of base2 ones and zeroes. Individual binary digits (bits) store a one or a zero. A one means true and a zero means false. A set of eight bits forms one byte. A byte can store one character in memory with ASCII, which allows 256 different characters. The newer, international Unicode stores one character in two bytes for a total of 65536 different characters. Neither this chapter nor this book explained any details about the operating system. Operating systems change frequently, or at least operating system versions change to a new-and-improved model about every two or three years. A solid knowledge of your computer's operating system is vital. Writing a computer program requires knowledge of editing text, saving and loading files and moving around the hard drive's directory system efficiently. If your basic computer knowledge is weak, make sure to pick up additional information from your teacher, library or bookstore. Technology is evolving and students arrive in computer science classes with increased sophistication. In this course no prior knowledge whatsoever about any programming language or programming logic is assumed. However, a fundamental knowledge of basic computer operations is essential. Sun Microsystems created Java to be a programming language that is portable on many computer platforms, a so-called platform-independent language. They also wanted the language to be compatible with web page development. Early computers were stand-alone work stations. The first networked computers used a "peer-to-peer" network. This was followed by LANs (Local Area Networks) that connected dedicated specialty servers with computers and printers for a common purpose. The Department of Defense developed the Internet as a means to provide communication at war time. Individuals, schools and businesses can set up a LAN at their private location without paying a fee beyond the cost of the necessary hardware and software. Connection to the Internet requires an ISP (Internet Service Provider) and a monthly connection fee. Many computers today, especially laptop computers, have wireless network connections. Such connections are convenient, but they are not as reliable and there is also a greater security risk. Computers use hardware and software. Hardware peripheral devices are the visible computer components. There are external peripheral devices, such as monitors, keyboards, printers and scanners. There are also internal peripheral devices like disk drives, CD ROM drives, network interface cards and video cards. Software falls into two categories of application software and operating system software. Application software includes the common applications of word processing and spreadsheets, but also tax return software and video games. Operating system software runs the computer and allows the user to personalize the computer to his or her needs and organize data. 1. According to the author, what is one of the most important elements of learning in a classroom? The student-teacher relationship What is the exposure equation? Bewilderment + Exposure = Obvious What will your time in the computer lab primarily be devoted too? Writing computer programs How is writing a computer program different from writing an essay? You have to follow very precise rules of writing Why do some people experience roller coaster emotions in computer science? People get excited when a program works and depressed when it does not. Learning computer science, like learning most lab sciences, is most effective when you do what? Learn by observation 9. 7. What is the first step in learning computer science? Read the assigned textbook pages During the lecture, what topics should you pay particular attention too? The ones that confused you What should you do if you are confused? Ask questions What should you do after the lecture? Reread the textbook pages What should you do before a scheduled lab? Read the lab requirements What should you do on a lab assignment when you are stuck? Get help Does cramming work in computer science? No

8. 2.

3.

10. 4.

11.

5.

12.

6.

13. 14.

List 2 problems with copying another students lab and turning in as your own. You do not learn & It is not ethical

31. 15. What computing device was invented in 3000 B.C. and is still used today in some parts of the world? The Abacus 32. 16. Who invented the Slide Rule? William Oughtred 33. 17. In your math class, you may have learned that Blaise Pascal created Pascals Triangle. What is he famous for in Computer Science? He built the first numerical calculating machine. 34. 18. Looms already existed in the 1800s. What was special about Jacquards Loom? Joseph Jacquard invents flexible cards that are punched with information in such a manner that it is possible to program how cloth will be weaved. It was one of the first examples of programming. Who invented the first general purpose computing machine? Charles Babbage Who is considered The Mother of Programming? Ada Lovelace Who invented a machine used for the US Census and later founded a company that eventually became IBM? Herman Hollerith Back in 1946, what would a programmer have to do to program the ENIAC? Walk inside the computer. What was the first programming language? FORTRAN commercially successful 4.

List 3 areas where computers are superior to human beings. Faster, better memory and more accurate What is really the cause of most computer errors? Human programming error or data input error If you have important information saved on a disk, what should you do to protect against the possibility of losing this information? Back up the data What is Morse Code based on? Long and short signals How many different combinations of on and off are possible with 8 lightbulbs? 256 What number system do you use? Base 10 What number system does the computer use? Base 2 What does ASCII stand for? American Standard Code of Information Interchange What ASCII range is used for the standard set of characters? 0..127 What ASCII range is used for the extended set of characters? 128..255 What is bit an abbreviation for? BInary digiT How many bits are in a byte? 8 How is Unicode different from ASCII? It uses two bytes and adds many characters What is a nibble? 4 bits How many different combinations are possible with 2 bytes? 65,536 Early computers used vacuum tubes. information did one vacuum tube store? One bit What eventually replaced the vacuum tubes? Transistors What do computers use today to store information? Planar transistors on microchips Look up the following 4 conversions the chart in section 1.10: 10 in base 10, 2 in base 2, 16 in base 16, & 5 in base 5. What do they all have in common? They all are represented by 10 How much

35.

36. 19.

37.

20.

1.

21.

2.

22.

3.

23.

24.

Jack Kirby invented something back in 1958 which eventually lead to the development of microchips. What did he invent? The Planar Transistor What was the first programming language designed for business? COBOL What was the commercially successful computer? The Apple II personal

5.

6.

25.

7.

26.

8.

27.

What was the first wide spread software to be sold? VisaCalc What company created the first personal computer to be considered a true business computer and not just a toy for playing games? IBM What company created the first operating system for IBM, and eventually became bigger than IBM? Microsoft What was the first commercially successful computer with the mouse/windows technology? The Macintosh (Mac)

9.

28.

10.

11. 29.

12.

30.

13.

What number system is typically used to display memory addresses? Base16 What is the main board in a computer, with all the primary computer components, called? Motherboard What are computer chips made out of? Silicon When a metal is a semiconductor that means it allows you to precisely control the flow of what? Electrons What kind of chip stores permanent information for the computer? ROM What does ROM stand for? Read Only Memory What kind of chip stores temporary information for the computer? RAM What does RAM stand for? Random Access Memory

32.

What is the name of the translator used by Assembly Language? Assembler List 2 of Grace Hoppers accomplishments. Develop program translators Develop COBOL Explain the difference between an interpreter and a compiler. Compilers translates the entire file and then execute. Interpreters translate each line and execute each line. What is a low-level language? Close to computer language What is a High-level language? Close to human language Why do we not simply use English, or any other human language, to program a computer? English is too ambiguous What was the first successful programming language for the mathematics and scientific community language? FORTRAN What does FORTRAN stand for? Formula Translator What community was COBOL created for? Business What does COBOL stand for? Common Business Oriented Language What language attempted to be a language that was good for science and for business, but turned out to be far too cumbersome? PL-1 Who was BASIC designed for? Beginner students What does BASIC stand for? Basic All-purpose Symbolic Instructional Code What was the first language used for personal computers back in the 1970s? BASIC Who developed Pascal? Niklaus Wirth Refer to the previous questions. Why did he develop Pascal? To teach structure programming in college. What does OOP stand for? Object Oriented Programming How is C++ different from the original C? It added OOP What does platform independent mean? A program can execute on any computer When was Java released and by whom? Sun Micro Systems in 1995

14.

33.

15.

34.

16.

35.

17.

36.

18. 19.

37.

1.

20.

2. 21. Which stores more information, gigabytes or terabytes? Terabytes 3. 22. Exactly how many bytes are in a kilobyte? 1024 4. 23. What does CPU stand for? Central Processing Unit 5. 24. The CPU is essentially the _______ of the computer. Brains Iron Oxide is the technical term for something we use to store information on a disk. What is the more common term? Rust What secondary storage device is coded with areas that reflect and absorb laser light? CDs Is a protractor digital or analog? Analog 9. 28. What type of information allows you to make a precise copy of the original? Digital Define the word Program. A set of instructions to achieve a specified goal. 11. 30. Explain why programming in machine language is undesirable. Programming is 1s and 0s is very tedious and problemetic. What language uses commands with mnemonic names and hexadecimal numbers? Assembly language

25.

6.

7. 26.

8.

27.

10.

29.

12.

31.

13.

14.

15.

Were early personal computers networked? No What is Sneaker Net? The old way information was shared by copying data to a disk, putting on your sneakers, and walking it to the other computer. The first practical networks for personal computers were called what? peer-to-peer networks

26.

16.

Refer to the previous question. List another problem. Information that travels wireless is much easier to pick by hackers than information that is channeled through cable. What does hardware involve? Hardware involves all the computer components that can be seen, felt, picked up and dropped, like a monitor, a mouse, a jump drive. What does software involve? Software involves the set of computer instructions that are coded on a disk, a CD, or a hard drive. What are external peripheral devices? These are hardware, located outside the computer and connected with some interface, which is usually a cable. Refer to the previous question. Can these be wireless? Yes What does CRT stand for? Cathode Ray Tube What does LCD stand for? Liquid Crystal Display How are internal peripheral devices different from external peripheral devices? They are connected to the computer inside the computer case. What does computer software do? Computer software provides instructions to a computer. Spreadsheets and word processors are examples of what kind of software? Application Software What does system software involve? System software involves the instructions that the computer requires to operate properly. List 3 major operating systems. Windows XP, UNIX and MAC OS

27.

17.

28. 18. What is a peer-to-peer network? a small group of computers with a common purpose all connected to each other 29. 19. What does LAN stand for? Local Area Networks What is a server? A server is a specialty computer that is connected to the LAN for one or more purposes. Servers can be used for printing, logon authentications, permanent data storage and communication. The Internet came about during what period in our history? The Cold War Where in the Internet is the central location where all the control computers are located? That does not exist. If it did it would defeat the entire purpose of the Internet. 34. 23. Normally, businesses and schools have a series of LANs that all connect into a large network called what? an Intranet What does ISP stand for? Internet Service Provider 36. 25. Wireless connections are convenient, but there are some problems. List one. Signals are not always reliable, just like cell phones. 37. 30.

20.

31.

21.

32.

33. 22.

35.

24.

Introduction to Java Sun Microsystems provides free software to compile and execute Java programs. The Java Development Kit (JDK) can be found on Sun's website at http://java.sun.com. The current version (June, 2007) finished by Sun is Version 1.6.0 update 1. The Java program language is designed to create full-fledged program applications for industry, as well as applets that will operate inside a web page. Normally, an application is a larger program than an applet. However, an applet is not necessarily a small program. The secret of Java's success is the use of both a compiler and an interpreter. First, the Java compiler is used to create an intermediate stage in program translation, called bytecode. Second an interpreter is used to translate and execute the bytecode. Sun created multiple interpreters for different platforms. The interpreter is called a Java Virtual Machine (JVM). There are three distinct steps in creating a Java program. The Java compiler and Java interpreter translate and then execute existing programs. You still need some tools to write the Java source code. It is possible to write code with any text editor and then use the command prompt to provide commands for compiling and executing. It is easier to use an Integrated Development Environment (IDE) like JCreator to assist in program writing. JCreator can be downloaded for free if you use the light edition. JCreator can execute both application programs and applet programs. You need to a special web page that imports the Java bytecode applet file. The different components of a program were explained and you saw that a program uses keywords in program statements. Keywords have special meaning to a program. In particular, you looked at the keywords System.out.print and System.out.println. Both keywords display text output. Keyword println adds a carriage-return/line-feed (crlf) to the output statement. The Java compiler checks to see that every program statement is written with correct syntax before a bytecode file is created. When mistakes are made, the compiler generates error messages about these mistakes. Computers must be used in a responsible manner. First, care must be taken that a computer is not harmed physically or stolen. Computers are vulnerable and can be easily damaged. Computer data is stored in areas that can be easily corrupted and all important data needs to be backed up in one or more storage areas, like hard drives, CDs or memory sticks. Computers also need up-to-date protection programs against viruses, spyware and SPAM. Using copyrighted software may be simple, but it is unethical. All software has license agreements that state if the software is free, licensed for one user, multiple users or an entire site, like a school or company. Many people hack into computers, supposedly for fun, unaware that they are inflicting damage with unknown computer hacking programs. 1. Explain why the United Nations 2-step translation 11. Refer to the previous question. Java then continues and process requires fewer translators than a 1-step uses an _______ to translate the _______ into executable translation process. _______ line by line. You only need 1 translator for every language and interpreter bytecode machine code English. 12. What is the name for the interpreter that takes the 2. When is a programming language considered to be bytecode and then executes the program? platform independent? JVM When a program created on one computer can execute on all other computers. 13. List 2 benefits with using Java on a web page? It is secure and it is interactive 3. What type of code is created by a Java compiler? Bytecode 14. What does HTML stand for? Hyper Text Markup Language 4. What is bytecode? The intermediate code generated by the Java compiler 15. What is an applet? A java program that executes inside a web page. 5. If you were to compare the executing of a Java file with the UN, how would bytecode fit in with the UN 16. What is an application? analogy? A program that executes stand-alone Bytecode is English 17. Can a website contain multiple applets? 6. Does the computer understand bytecode? Yes No 18. Can a website be written entirely in Java? 7. What does Java use an interpreter for? No To translate and execute bytecode 19. What 3 things do you need to write a Java program? 8. What does JVM stand for? Text editor, compiler and interpreter Java Virtual Machine 20. Refer to the previous question. Some software packages 9. Which program is larger, a compiler or an interpreter? combine all 3 of these things into an IDE. Compilers are much larger. What does IDE stand for? Integrated Development Environment 10. Java uses a _______ to translate the program source 21. Can Notepad be used to write a Java program? code created by the programmer into _______. Yes compiler bytecode

22.

All Java programs end with what suffix? .java Can the Java compiler be accessed from the Command Prompt? Yes All bytecode files end with what suffix? .class What are the 3 steps of a Java program? (This counts as 3 questions.) Write the Java source in some text editor. Translate the source code file with a Java compiler into an intermediate bytecode file that will end with .class. Execute the bytecode file with a Java Virtual Machine (JVM) program, which is an interpreter.

6.

What does the command System.out.println do? It displays the characters placed between the double quotes that follow. What is a Java keyword? A word that has a special meaning to Java Two or more keywords combine to make what? a program statement What does it mean when we say that keywords in Java are case-sensitive? That means that print and Print are two different words to Java. List 7 keywords. public, class, static, System.out.println.

23.

7.

24.

8.

25-27.

9.

10.

void,

main,

String

and

28.

What does URL stand for? Universal Resource Locator What does JDK stand for? Java Development Kit What does JRE stand for? Java runtime Environment

11.

What does CRLF stand for? Carriage return, line feed Explain the difference between print and println. The print keyword generates output without a carriage return and the println keyword does include a carriage return. What does println() do when there is nothing between the (parentheses)? Skip a line Both print and println follow what Java keyword(s)? System.out If you see public class Jessica in your program, what name must the file have? Jessica.java What punctuation symbols must be at the end of all Java program statements? ; Physical damage to a computer happens, but it is not the biggest problem. The number one source of grief for many people, especially students, is _______________________. loss of data What does RAM stand for? Random Access Memory What happens to the RAM if the computer loses power? It is lost. What does AVR stand for? Automatic Voltage Regulation What are the 2 parts of a computer virus? The duplication part and the payload What is spyware? Special programs that snoop around your computer and try to record information Why is it bad to leave your computer both logged in and unattended? Anyone can go to your computer and copy, alter or erase anything. What does USB stand for? Universal Serial Bus

29.

12.

30.

13. 31. Why is the JRE necessary? Allows executing Java programs How much does it cost to download Java from Sun Microsystems? It is free When downloading the JDK from Sun Microsystems, what is the difference between downloading the Offline or the Online installation file? The Online choice downloads a very small file with instructions to download additional information during software installation. The Offline choice downloads all the necessary files required for installation. This means that you can then install the software and it is not necessary to be connected to the Internet. Java is an island in what country? Indonesia What is the difference between the PRO version and the LE version of JCreator? The PRO version includes sophisticated debugging tools for the professional programmer. The LE version is free. What does GUI stand for? Graphics User Interface Look at figure 2.39. The first 5 lines of program Java0201.java all begin with //. What does that mean? These are all comments. Are comments compiled into bytecode? No Every application program has a _______________ segment. main 18. 14.

32.

15. 33.

16.

17.

34.

1.

19.

2.

20.

3.

21.

22.

4.

23.

5.

24.

Java Simple Data Types This chapter introduced the Java simple data types. A simple data type is simple because it stores a single value in memory. Simple data types are also called primitive data types. Program examples were shown that declared variables of a specified data type. Declaring the data type allows the compiler to allocate memory for the value to be stored. There are four different integer data types: byte-1, short-2, int-4 and long-8 each indicated with their respective number of bytes in memory. Java provides five operators for integers: addition, subtraction, multiplication, integer-division and modulus-division. There are two different real number data types: float-4 and double-8. Float is short for floating point number and double indicates that this data type is twice the precision of a float data type. Java provides four operators for real numbers: addition, subtraction, multiplication and real number division. The specified data type is good for memory efficiency, but stingy use of memory can result in memory overflow. If a value is larger than the space reserved in memory, the result is incorrect, even if the mathematics is flawless. In Java there are many shortcut notations for both unary operators and binary operators. Every arithmetic operator can be expressed in a shortcut notation. Keep in mind that multiple shortcut operations in the same statement can create very ambiguous program statements that are difficult to predict. Java can declare character and string variables. The plus operator is used for arithmetic addition with numbers and concatenation with strings. Concatenation means that a string is appended at the end of another string. The String data type is included with the simple data types because we treat it like a simple data type right now. A string does hold multiple character values, but we process the entire set of characters as a single unit. You will learn in a later chapter the true nature of the String data type. This chapter also introduced the boolean data type. This data type can store the value true or the value false. Boolean is included with this chapter to make the chapter complete with all the available simple data types. The actual usage of boolean will be shown later. Java has a peculiar variable, called a final variable that cannot change. I prefer to call this a constant. Declaring a constant is identical to declaring a variable with the reserved final in front of the data type. It is important to document your programs. Start by selecting identifiers that are self-documenting. Single-character identifiers should be avoided in most cases. Programs should also make generous use of meaningful comments that help explain the purpose of program segments and certain program statements. Java programs use the same mathematical precedence that is used in mathematical computation. Logically, there is no apparent difference between mathematics and computers science. Practically, there are some differences. In mathematics there are assumed operations, especially multiplication that needs to be explicitly shown in a Java program. It is sufficient to state AB + CD in mathematics. In Java such an expression needs to be A*B + C*D. It is possible to alter data types with type casting. Integers can become doubles and characters. Doubles can become integers and characters. Characters can become integers and doubles. Type casting is achieved by placing the new, desired data type inside parentheses in front of the variable to be altered. Literal strings will not always display literally. Certain special characters following a single slash like \n, \t will result in a carriage return, a tab or some other special output display. The slash-character combination is called an escape sequence. 1. A program is made up of words, which usually are called what? Keywords What is the first Java syntax rule? Use only keywords known by the Java compiler. List the 3 categories of keywords. Reserved words, pre-defined identifiers and user-defined identifiers List 3 examples of Java Reserved Words. public, static, void Java has a large number of libraries that enhance the basic Java language. These libraries contain special program modules that perform a variety of tasks to simplify the life of a programmer. What are these modules called? methods List 2 examples of Predefined Identifiers. print and println 7. When you are creating User-Defined Identifiers, you must make sure your identifier is not one of what two things? A reserved word or a pre-defined identifier What are the rules for naming an identifier? Use alpha-numeric characters and start with an alpha character. Print the Java statement that will declare x as an integer. int x; Refer to the previous question. Print the statement that will assign the value of 7 to x. x = 7; In program Java0301.java, why does the statement: System.out.println(a); display the value of 10 and not a? The letter is not between double quotes. Why does program Java0302.java not compile? There are no values assigned to the variables.

2.

8.

3.

9.

4.

10.

5.

11.

12. 6.

13.

Print the Java statement that will declare x as an integer and assign the value of 7 to x in one single statement. int x = 7; List Javas 4 integer types. byte, short, int and long

11.

What does x *= 5; mean? Multiply x by 5 What does x /= 5; mean? Divide x by 5 What does x %= 5; mean? Compute the remainder or dividing x by 5

12. 14.

13. 15. How many bytes are used by an int? 4 What is the largest value of a byte? 127 Would a short be appropriate to store Zip Codes? No Explain your answer to the previous question. A short cannot hold any number above 32,767. Many zip codes are larger than this. What are the 5 integer operations? Addition, subtraction, multiplication, integer division and remainder division What is the difference between the / and the % division operators? / is integer division and % is remainder division 15.

16.

14.

17.

What is the difference between the char and String data types? The char type store one character and the String data type store one or more characters. In program Java0312.java, what is accomplished by the statement: c1 = c2 = c3 = 'Q'; ? Every variable is assigned the value of Q. What is the fancy name for using the plus ( + ) sign to join strings together? Concatenation The plus ( + ) sign can be used to add integers and real numbers. It can also be used to join strings. What is it called when one operator can perform different functions. Overloading What is 2 + 3 ? 23 Who invented a form of Algebra based on logical statements that are either true or false? George Boole Today, in computer science, a data type that has only two values of true and false is a called a _______ data type. boolean What is Javas formal language for the term simple data types. primitive Say you want to use PI in your program with value of 3.14159. This value will never change in your program. Print the proper way to define and initialize this constant. final double PI = 3.14159; What happens when you attempt to alter the value of a final variable? It causes a compile error. What is a self documenting identifier? An identifier, such as grossPay, that explains the purpose of the variable. What is the difference between double-slash ( // ) comments and slash-star star-slash comments (/* */) ? // is a single line comment /* */ is a multiple line comment Does Java follow the same Order of Operations that you learned in your Math Yes

18.

19.

16.

20.

17.

1.

In binary, what indicates if a number is positive or negative? If the first bit is 0, the number is positive and if the first bit is 1, the number is negative How is it possible for a computer to multiply 2 positive numbers, and get a negative product? Memory flow, which alters the sign bit

18.

19. 2.

20. 3. Explain Memory Overflow. A condition that occurs when the mathematical value is too large to be stored in the actual computer memory space. 21. 4. The Real Number data type that we will use is called double. Why is a double called a double? It has double the bytes of a float 22. 5. Which is more precise? A double, or a float? Double

Assume x is an int for the next several questions. 23. 6. What does x++; or ++x; mean? In both case variable x is incremented by 1. What does x--; or x; mean? In both cases variable x is decremented by 1. Should Java shortcuts be combined with other Java statements? Example: System.out.println(x++); No, it causes confusion What does x += 5; mean? Increase x by 5 26. 10. What does x -= 5; mean? Decrease x by 5 24.

7.

8.

25.

9.

27.

Translate 7abc into Java source code. 7*a*b*c 32.

3.333333 What is an escape sequence? A special set of characters, starting with /, that means something to print, like \n What does the escape sequence \n do? carriage return, line feed What escape sequence is used to generate a <tab>? \t Why do we need escape sequences to generate backslash ( \ ) and quote ( " ) characters? It prevents confusion to the compiler What are the 4 data types that will be tested on the AP Exam? int, double, boolean and String

28.

Translate

x+7 into Java source code. 2x - 3

33.

34. (x + 7) / (2 * x - 3) 35. 29. Translate one half into Java source code. 1.0 / 2.0 or 0.5 What is the output of: System.out.println( 10 / 3 ); ? 3 What is the output of: System.out.println( (double) 10 / 3 ); ? 36.

30.

31.

Using Methods and Parameters This chapter introduced Object Oriented Programming, known as OOP. The biggest concern to professional programmers is program reliability. The switch to Object Oriented Programming has increased the reliability of large, complex programs considerably. It will take many more chapters to appreciate why Object Oriented Programming makes programs more reliable and makes programs easier to write, debug and enhance as well. The importance of OOP makes it necessary to start thinking about objects and working with objects early on in the course. Learning OOP starts early, but it will take the entire course. A gradual, steady introduction of new OOP topics throughout the course will make Object Oriented Programming manageable, rather than bewildering. You will not only find new OOP topics introduced in many chapters to come, but you will also see frequent repetitions of topics that were already introduced. The key point introduced here is the notion that objects have capabilities that are neatly wrapped inside a container. Both the data and any special actions that need to access the data are placed inside the same container or capsule. This is known as encapsulation. A general container with data and actions, known as methods, is called a class. A class is a data type. One particular variable of a class is an object. The first methods introduced were methods of the Math class. Access to methods is done by using the class identifier, followed by a period and then the method identifier, like Math.sqrt(16). This approach is done with Math methods. This chapter concluded with methods of the Graphics class. Each one of the program examples created an applet, which can only be viewed with a small web page that includes the byte code file of the applet. Methods like drawLine, drawRect and other Graphics methods are called by using a variable object identifier of the Graphics class. In the program examples this variable normally was g, but it can be any name. Access to Graphics methods and Applet features requires that Java libraries are "imported" into the program. Using statements at the top of the program like: import java.awt.*; import java.applet.*; gives the Java compiler the required information to use the methods in the program and create an applet for a web browser. 1. What does OOP stand for? Object Oriented Programming What is the difference between C++ and Java with regards to OOP? C++ gives you the option to use OOP. Java requires it. What are the 4 stages of program design? Cryptic Programming Stage Unstructured, SpaghettiProgramming Stage Structured Programming Stage Object Oriented Programming Stage Why were some programs intentionally written very cryptic in the 1st stage of program design? Many programs were written intentionally in a very cryptic style that only the creator of the program could understand. 5. Why did the first stage of program design not last long? Computers became cheaper and programmers became more expensive. Programs required frequent debugging and updating, and the cryptic style usually meant starting a program from scratch if the creator left the company. Companies could not afford this wasteful approach. What quick fix statement was used in the 2nd stage of program design? goto

2.

3.

6.

4.

7.

Refer to the previous question. How did this statement lead to the term spaghetti programming? If you use a pencil and try to draw the flow of a program that uses goto, what you wind up with rapidly looks like spaghetti. Programs in the past and today have one common goal that allows no compromise. What is that? reliability Is GOTO used in structured programming? No In structured programming, program flow is strictly controlled with how many entrances and exits? one entrance, one exit In structured programming, program statements with a common purpose were grouped together in special _____________. modules In structured programming, how many tasks per module? one Structured programming greatly improved the _____________, _____________, and _____________, of programs. creation, debugging and updating Why are computer programs so much longer today that they were in the late 1980s and early 1990s? To be more user friendly, computer programs are now GUI based rather than text based. OOP incorporates what 3 features? encapsulation, polymorphism and inheritance. In OOP, what are attributes used for? Attributes store program information, which is like a noun In OOP, what are methods used for? Methods perform some actions on the information, which is like a verb. What 2 Java methods have you already learned about? print & println What is encapsulation? The process of putting attributes and the methods that act upon those attributes in the same container (object). What does the word polymorphism mean? many forms Explain inheritance in computer science terms. Inheritance allows one class to inherit the attributes and methods of another class without have to rewrite them. Modules that perform a related set of functions are grouped together in a special program type of container. What is this container called? class What 2 things do classes contain? data & methods

24.

What are the 2 types of methods in Java? class methods & object methods What is the output System.out.println(Math.sqrt(100)); ? 10 of

25.

8.

26.

Refer to the previous question. What does Math signify? The name of the class Refer again to question 25. What does sqrt signify? The name of the method Refer yet again to question 25. What does 100 signify? The argument/parameter/information being passed/sent to the method What is a parameter used for? It provides necessary information to the method. Can a method be an argument for another method? Yes The information, which is passed to a method is called an _____________ or a _____________. argument or a parameter Where are parameters placed? Parameters are placed between parentheses immediately following the method identifier. What 4 things can parameters be? Parameters can be constants, variables, expressions or they can be methods. The floor, ceil, and round methods of the Math class all round in some way. What is the difference among them? floor always rounds down ceil always rounds up round rounds normally What is the output of System.out.println(Math.max(100,50)); ? 100 What is the output of System.out.println(Math.min(100,50)); ? 50 What is the output of System.out.println(Math.abs(-7)); ? 7 What is the output System.out.println(Math.pow(3,4)); ? 81 What is the output System.out.println(Math.pow(4,3)); ? 64 Are PI and E attributes or methods of the Math class? attributes Which class has methods that use more parameters: the Math class or the Graphics class? The Graphics class of

9.

27.

10.

28.

11.

29.

30.

12.

31.

13.

32.

14.

33.

15.

34.

16.

17.

35.

18.

36.

19.

37.

20.

38.

21.

39.

of

22.

40.

23.

2.

3.

What method is not found in an applet? main What controls the execution sequence in an applet? The paint method How is calling a method of the Math class different from calling a method in the Graphics class? The Math class starts with Math and the Graphics class starts with g. What library must be imported in order to use the Graphics class? awt

12.

4.

What do the 4 parameters of the drawOval method of the Graphics class specify? You provide the parameters for the rectangle that circumscribe the oval. What is the difference between drawOval and fillOval? Method drawOval only draws lines and method fillOval fills the shape. How do you create a circle with the drawOval and fillOval methods? Use the same value for width and height. What is an arc? Part of an oval The 1st 4 parameters of the drawArc method are the same as the drawOval method. What do the 5th and 6th parameters specify? The start of the arc and the size of the arc When you use parameters incorrectly it may not always be apparent that the program has some __________ error. logic Assume g is an object of the Graphics class. Print the Java command to change the graphics color to orange. g.setColor(Color.orange); Color constants are combinations of what 3 colors? red, green and blue What Graphics method is similar to println, but used with graphics output? drawString

5.

13.

14. 6.

15. 7. What does awt stand for? abstract window tool What do the 4 parameters of the drawLine method of the Graphics class specify? The start and end coordinates 17. 9. What do the 4 parameters of the drawRect method of the Graphics class specify? The left-top corner coordinate and the width and the height of the rectangle 18. 10. What is the difference between drawRect and fillRect? Method drawRect draws lines only and method fillRect fills in the shape. 19. 11. How do you create a square with the drawRect and fillRect methods? Use the same value for width and height. 16.

8.

20.

Control Structures This chapter introduced controls structures. Prior to this chapter, programs were strictly controlled by the sequence of program statements. Java, like most programming languages, provides three types of control structures. There is simple sequence, selection and repetition. Simple sequence provides control by the order in which program statements are placed in a program. This is very important, but very limited control. It is not possible to make any decisions nor is it possible to repeat any block of program statements. Input at from the keyboard is accomplished with a variety of Java statements that will be explained in greater detail in a future chapter. You do need to realize that method nextLine enters strings, method nextInt enters integers and method nextDouble enters double data types. Selection provides three types of decision control structures. There is one-way selection with if, two-way selection with if..else and multipleway selection with case..break. Iteration also provides three types of loop control structures. postcondition iteration with do..while. There is fixed iteration with for, pre-condition iteration with while and

This chapter is not the complete story. Every program example uses only a single condition with selection and iteration. It is possible to use multiple conditions with control structures and it is also possible to nest one control structure inside another control structure. These features will be shown in a later chapter. This chapter concluded by returning to graphics programming. You did not learn any new methods to create graphics display. However, you did see examples of how control structures can enhance the appearance of graphic displays. The last two program examples introduced some simple computer animation with the draw and erase technique. This is not an impressive-looking animation technique, but it is a start that can be comprehended at this early stage. 1. What does program flow follow? The sequence of written program statements Programs in what computer language require control structures? Java and all other program languages What are the 3 general types of control structures? Simple sequence, selection and repetition 15. 4. How does Simple Sequence work? Program statements are written in the sequence that they should be executed. What are 2 synonyms for Selection? Decision making and branching What are the 3 types of Selection? One-way, two-way and multiple-way Selection control structures _____________ statement. condition use a special 19.

13.

2.

What do Selection and Repetition control structures have in common? They both use conditional statements. What is the essence of understanding, and using, control structures? Use the correct control structure for the correct purpose. What is a conditional statement? A statement that is true or false Most conditional statements will require what? A logical or Boolean operator All conditions must be placed inside what? Parentheses What kind of operator is required to make an expression evaluate to true or false? A logical or Boolean operator List the 6 relational operators. == > < >= <= != Java does not use = to test for equality? What does it use instead? == What kind of operator is =? An assignment operator What library is the Scanner class located in? the java.util library Why is a prompt necessary for program input? Without the prompt the program user has no clue what is happening and certainly does not know what type of input is required.

14.

3.

16.

5.

17.

6.

18.

7.

8.

In one-way selection, when does the program flow branch off? If the condition is true In one-way selection, what happens if the condition is false? The program continues and ignores the if statement. In two-way selection, what happens if the condition is false? The program executes the else statement. Explain Multiple-Way Selection. A variable value is compared against multiple values. When a match occurs, the statement with the match is executed. Explain Repetition. Repetition repeats the same statement or statements as long as some condition is valid.

9.

20.

10.

21.

11.

22.

23. 12.

24.

What does the nextLine method do? The nextLine method "reads" in an entire string of characters from the keyboard until the <Enter> key is pressed. Look at program Java0503.java. This program gives the impression that it will find the sum of 2 entered numbers. Why does it not work? Since the numbers were entered as Strings, they were concatenated instead of added. Look at program Java0504.java. How does this program cure the problem of the previous one? It used nextInt to enter int values. What method is used to enter real number information? nextDouble What is the simplest control structure? for Write the Java statement that will do this action: If your grade is 70 or better, print "You passed!" if (grade >= 70) System.out.println("You passed"); Why should indentation be used with control structures? It makes the program more readable. Explain the unusual output of program Java0507. The program is meant to execute multiple statements only if the condition is true. What do you need to add to a program if you wish to control multiple statements with if. Create block structure with opening and closing braces.

12.

What would be the output of program Java0512 if B was entered? Enter Letter Grade ===>> B 80 .. 89 Average What are 2 synonyms for Repetition? Iteration and looping What loop structure is used with fixed iteration? for In fixed iteration, what does fixed mean? The number of times that the body of the loop structure executed. What would you need to change in program Java0513 if Joe wanted the message displayed 1000 times? Add 940 additional output statements What would you need to change in program Java0514 if Joe wanted the message displayed 1000 times? Change the value in the loop condition What does LCV stand for? Loop Control Variable

25.

13.

14.

26.

15.

27.

16.

1.

17.

2.

18.

3.

Use this statement for questions 19-21: for( int j = 1; j <= 100; j++) 19. What does the j = 1 mean? It is the loop control variable What does the j <= 100 mean? Continue to execute the loop as long as this statement is true. What does the j++ mean? Increment the loop control variable by 1 for each iteration. How do you repeat multiple statements with the for loop structure? Use braces Must the loop counter in a for statement be an int? No Give a real life situation where fixed iteration is not practical. A loop that repeats until a password is entered What kind of loop is the while loop? It is a pre-condition loop The while loop structure requires a statement inside the loop body that was not necessary with the for loop. What is the purpose of this statement? A loop counter is necessary to exit the loop. What is the only thing checked in a while loop? The pre-condition Where must the loop counter be initialized when using a while loop? Before the loop condition is used What must happen inside a while loop? The loop counter must change

4.

20. 5.

21. 6. What statement is used with if for two-way selection? else Write the Java statement that will do this action: If your grade is 70 or better, print "You passed!" otherwise print "You failed!" if (grade >= 70) System.out.println("You passed"); else System.out.println("You failed"); How do you control multiple statements with two-way selection? Usebraces 25. 9. What would be the output of program Java0511 if B was entered? Enter Letter Grade ===>> B 80 .. 89 Average 70 .. 79 Average 60 .. 69 Average Below 60 Average While program Java0511 compiles and executes, its output is strange. How does program Java0512 fix the problem? It uses the break keyword. When is the default statement executed? When no match is found. 22.

7.

23.

24. 8.

26.

27. 10.

28.

11.

29.

The loop condition comes after the loop body. 30. Why is the do...while loop called a post condition loop? Determine the output for each program that follows. Write the output in the blank cell next to the program. public class Ex0501 { public static void main (String args[]) { for (int x = 0; x <= 8; x+=2) System.out.println("x = " + x); } } public class Ex0502 { public static void main (String args[]) { for (int x = -1; x < 8; x+=3) System.out.println("x = " + x); } } public class Ex0503 { public static void main (String args[]) { for (double x = 0; x < 4; x+=0.5) System.out.println("x = " + x); } } public class Ex0504 { public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x <= 25; x+=3) y++; System.out.println("y = " + y); } } public class Ex0505 { public static void main (String args[]) { int x = 10; int y = 0; for (x = 3; x > 0; x--) y++; System.out.println("y = " + y); } } X=0 X=2 X=4 X=6 X=8

X = -1 X=2 X=5

X=0 X = 0.5 X=1 X = 1.5 X=2 X = 2.5 X=3 X = 3.5 Y=9

Y=3

public class Ex0506 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 3) { y++; x = y; } System.out.println("y = " + y); } } public class Ex0507 { public static void main (String args[]) { int x = 1; int y = 1; while (x < 15) { y = x + 2; x = y + 3; } System.out.println("y = " + y); } } public class Ex0508 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 15) { y = x * 2; x++; } System.out.println("x = " + x); System.out.println("y = " + y); } } public class Ex0509 { public static void main (String args[]) { int x = 2; while (x < 10) { if (x % 2 == 0) x+=3; else x+=2; } System.out.println("x = " + x); } }

Y=3

Y = 13

X = 15 Y = 28

X = 11

public class Ex0510 { public static void main (String args[]) { int x = 2; do { if (x % 2 == 0) x+=3; else x+=2; } while (x < 10); System.out.println("x = " + x); } } public class Ex0511 { public static void main (String args[]) { int x = 5; int y = 15; do { x = y + 2; y = x - 2; } while (x < y); System.out.println("x = " + x); } } public class Ex0512 { public static void main (String args[]) { int x = 10; int y = 2; do { if (x % 2 == 0) x += 5; else y += 2; } while (y < x); System.out.println("x = " + x); System.out.println("y = " + y); } }

X = 11

X = 17

X = 15 Y = 16

public class Ex0513 { public static void main (String args[]) { int x = 3; int y = 5; int z = 9; while (z > x + y) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); } } public class Ex0514 { public static void main (String args[]) { int k = 0; int x = 2; int y = 3; int z = 4; for (k = 1; k <= 3; k++) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); System.out.println("k = " + k); } } public class Ex0515 { public static void main (String args[]) { int x = 216; int y = 120; int z = 0; do { z = x % y; if (z == 0) System.out.println("y = " + y); else { x = y; y = z; } } while (z != 0); } }

X = 14 Y = 23 Z = -9

X=7 Y = 11 Z = -4 K=4

Y = 24

1. What is the output? 2345

int x=1; while(x<5) { x++; System.out.print(x); }

2. What is the output? 14

int m=2, total=0; while(m<6) { total=total+m; m++; } System.out.print(total);

3. What is the output? 42

int z=2, sum=0; while(z<9) { z++; sum=sum+z; } System.out.print(sum);

4. What is the output?

0123

int k=3; String s=""; while(k>-1){ s=k+" "+s; k--; } System.out.print(s);

5. What is the output?

11 9 7

int b=5; String list=""; while(b<11) { b=b+2; if(b%2==1) list=b+" "+list; } System.out.print(list);

Using Object Methods This chapter continued the Object Oriented Programming introduction started in the last chapter with class methods. This chapter continued with an introduction to using object methods. A general container with data and actions, known as methods, is called a class. A class is a data type. One particular variable of a class is an object. There are class methods and there are object methods. This chapter is only concerned with using existing methods from existing classes, not creating any new classes and or new methods. The first methods, introduced in the last chapter, were the class methods of the Math class. Methods of a utilitarian style, like mathematical functions, are class methods and they are called by using the class identifier, like Math.sqrt(16). Most classes require that memory is allocated for each variable or object of the class that is constructed. The Bank class was used to demonstrate that there is need for one object to store account information for each customer of the Bank class. Objects are constructed with the new operator. The Bank class is a simple class that I created personally. It is not part of the large Java library of classes. The Random class is part of the Java library and belongs with a group of other classes to the util package, which in turn belongs to the java package. Packages organize classes for easy access. You need to use the reserved word import to access a desired package. This is not necessary with the java.lang.* package, which is automatically loaded by Java. The Math class belongs to the java.lang package. It is one of the most important Java packages. The Random class contains methods, which generate random integers and random real numbers. It is possible to generate truly random numbers, which will be different with each program execution. It is also possible to construct Random objects with a starting seed, which will guarantee the same "random" sequence each time the program executes. Random numbers can also be generated with the Math.random method. Output format can be controlled with methods of the DecimalFormat class. This class is part of the java.text package. With DecimalFormat and the format method it is possible to format the output of a number. You can pad zeroes in front and behind a number. It is also possible to insert commas and round off a real number to a desired number of digits beyond the decimal point. This chapter also introduced some graphics classes. Polygons can be created with objects of the Polygon class and the addPoint method. The drawPolygon method or fillPolygon method of the Graphics class actually draws the polygon after the object is created. Random objects can be combined with graphics objects to display objects at random location with random colors. With the release of Java JDK 5.0 the Scanner class is available for interactive, text-style, program input. The Scanner class is part of the util package. Keyboard input requires a Scanner object that is instantiated with the System.in parameter, like: Scanner keyboard = new Scanner(System.in) The nextLine() method is used to enter String information. The nextInt() method is used to enter int information. The nextDouble() method is used to enter double information. The keyboard entry method retrieves information from the keyboard buffer. Methods nextInt and nextDouble do not retrieve the carriagereturn-life-feed character, like the nextLine method. Using a dummy String variable to remove the crlf character solves this problem. 1. Objects contain data and what else? methods What does the word class imply? A data type that can contain data and methods List 7 Math class methods. abs, pow, sqrt, round, floor, ceil, min, max List 2 Math class data members. PI and E 9. 5. Methods of the Math class are called what type of methods? Class Methods What is wrong with program Java0601.java? The Bank class does not allow access in the same manner as the Math class. 7. Refer to the previous question. How does program Java0602.java fix the problem? You need to create an object of the Bank class to have proper access. Refer to program Java0602.java for questions 8 and 9. 3. 8. Refer to program Java0602.java. In the statement Bank tom; what is Bank and what is tom? Bank is the class and tom is the object. What does the statement tom = new Bank(); do? It instantiates a tom object of the Bank class For right now, the author wants you to think of constructing an object as a combination of what 2 things? Declaring a variable to be an object or a class and instantiating the object.

2.

4.

10.

6.

Refer to program Java0603.java for questions 11 through 14. 11. What Bank class method makes the checking and savings accounts start at $0.00 when a new Bank object is created? constructor Print 2 lines of Java code that will create a Bank object for yourself and initialize both your checking and your savings account to $1 million. Bank myAccount = new Bank(1000000,1000000); Print 1 line of Java code that will make a $250,000 deposit in your checking account. myAccount.changeChecking(250000); Print 1 line of Java code that will make a $500,000 withdrawal from your savings account. myAccount.changeSavings(-500000); Refer to program Java0604.java for questions 8 and 9. 15. What action is performed by the getCombined method? It returns the sum of the checking and savings account balances. What actions are performed by the closeChecking and closeSavings methods? It changes both account balances to zero. At the top of this hierarchy is the __________ package. java The most important _____________package. java.lang package is the

26.

In addition to the Random constructor, what other method is used to set the seed for computing random numbers? setSeed What method is used to produce random real numbers between 0 and 1? nextDouble Assume that rand is an object of the Random class for questions 28 through 35.

27.

12.

13.

28.

What range of numbers can be displayed with: System.out.println(rand.nextInt(90) + 10); ? 10 ... 99 What range of numbers can be displayed with: System.out.println(rand.nextInt(50) + 1); ? 1 ... 50 What range of numbers can be displayed with: System.out.println(rand.nextInt(120) + 60); ? 60 ... 179 What range of numbers can be displayed with: System.out.println(rand.nextInt(100)); ? 0 ... 99

14.

29.

30.

31.

16.

In questions 31 - 35, fill in the missing parameters to obtain the desired range of random #s. 32. Range [1..100] System.out.println(rand.nextInt(_100_____ _) + _1________); Range [0..100] System.out.println(rand.nextInt(_101_____ _) + __________); Range [20..60] System.out.println(rand.nextInt(_41______ _) + _20_______); Range [100..999] System.out.println(rand.nextInt(_900_____ _) + _100______);

17.

18.

33.

19.

Refer to the previous question. Do you have to import this package? No, it is automatically loaded In the statement import java.util.Random; what is the class and what is the package? The package is util and the class is Random All package identifiers start with a __________ letter and a class identifier starts with a __________ letter. lower-case upper-case

34.

20.

35.

21.

36. 22. What does the nextInt method of the Random class do? It returns a random integer Look at programs Java0605.java and Java0606.java. Why does the execution of the former produce different results each time while the execution of the latter produces the same results each time? Output is different with the default constructor. With a parameter constrcutor the seed value makes each output sequence the same Methods with _____________. Overloaded multiple capabilities are called 38.

Look at program Java0611.java. How does this program manage to display random characters? It generates random integers that are equated to ASC character values.

23.

For questions 37 - 40, rewrite the println statements from questions 32 - 35 so they use Math.random. 37 Rewrite #32: System.out.println( (int) (Math.random() * 100) + 1); Rewrite #33: System.out.println( (int) (Math.random() * 101) + 0); // + 0 is optional Rewrite #34: System.out.println( (int) (Math.random() * 41) + 20);

24.

25.

Speaking algebraically if n is the integer parameter of the nextInt method. What is the largest number you can get in return? n-1

39.

40.

Rewrite #35: System.out.println( (Math.random() * 900) + 100);

(int)

1.

What statement must be at the top of your program in order for you to use the DecimalFormat class? import java.text.DecimalFormat Look at program Java0616.java. What do the 5 zeros mean? Fill in leading zeros in front of the output number Refer to the previous question. What happens if the number being displayed has more than 5 digits? Output formatting is ignored

7. How many different shades of Red, Green, and Blue are possible? (Hint: Look at program Java0621.java.) 255 8. In the statement: Bank tom = new Bank(2000); What is Bank and what is Tom? Bank is a class and tom is an object What kind of object is created by this statement: g.setColor(new Color(255,0,255)); A new color object that will set the color to purple Refer to the previous question. Are the same type of objects used in program Java0622.java ? No, there are three different objects What are the 3 steps in creating a Polygon in Java? Create a Polygon object. Add coordinate points to the Polygon object Use the Polygon object in a drawPolygon or fillPolygon method call Look at program Java0623.java. What do you need to do to this program to make the output a hexagon? Add one more coordinate point

2.

3.

9.

4.

Print the Java code necessary to create a DecimalFormat object called output that will that will display an integer like 1234567 as 1,234,567 DecimalFormat output = new DecimalFormat("0,000,000"); Print the Java code necessary to create a DecimalFormat object called output that will that will display a real number like 12.34567 as $12.35 DecimalFormat output = new DecimalFormat("$0.00");

10.

5.

11.

6.

Print the Java code necessary to create a DecimalFormat object called output that will that will display a real numbers rounded to 3 decimal places. DecimalFormat output3 = new DecimalFormat("0.000"); 13. When creating a Polygon, does the order in which you add the coordinate points matter?

12.

Yes

In questions 14 through 16 use the provided grid to draw the program output. 14. What output would be displayed by this program? import java.awt.*; import java.applet.*; public class Question14 extends Applet { public void paint(Graphics g) { Polygon x = new Polygon(); x.addPoint(100,200); x.addPoint(300,100); x.addPoint(500,100); x.addPoint(700,200); x.addPoint(700,300); x.addPoint(500,400); x.addPoint(300,400); x.addPoint(100,300); g.drawPolygon(x); } } 0 100 200 300 400 500 599 100 200 300 400 500 600 700 799

15.

What output would be displayed by this program? import java.awt.*; import java.applet.*; public class Question15 extends Applet { public void paint(Graphics g) { Polygon x = new Polygon(); x.addPoint(100,200); x.addPoint(300,100); x.addPoint(500,100); x.addPoint(700,200); x.addPoint(700,300); x.addPoint(500,400); x.addPoint(300,400); x.addPoint(100,300); g.fillPolygon(x); } } What output would be displayed by this program? import java.awt.*; import java.applet.*; public class Question16 extends Applet { public void paint(Graphics g) { Polygon x = new Polygon(); x.addPoint(300,400); x.addPoint(300,100); x.addPoint(700,300); x.addPoint(100,300); x.addPoint(500,100); x.addPoint(500,400); x.addPoint(100,200); x.addPoint(700,200); g.drawPolygon(x); } }

0 100 200 300 400 500 599

100

200

300

400

500

600

700

799

16.

0 100 200 300 400 500 599

100

200

300

400

500

600

700

799

Hint: If you are confused on questions 14 through 16, simply plot the points on the grid and then connect the dots in sequence. Refer to program Java0625.java for questions 17 through 19. 17. How would you change this program to display 500 random lines? Alter the value in the for condition to 500 What would happen is the 2 800s are changed to 400s? The lines become shorter and will draw on the left side of the screen What would happen is the 2 600s are changed to 300s? The lines become shorter and will draw on the top half of the screen What Scanner method is used to enter a String? nextLine 21. What Scanner method is used to enter an int? nextInt What Scanner method is used to enter a double? nextDouble Look at program Java0630.java. What is wrong with this program? Not all input appears to be entered properly Look at program Java0631.java. How does this program cure the problem of the previous program? The dummy variable absorbs the return character to allow the next input to work correctly.

22. 18.

23.

19.

24.

20.

Anda mungkin juga menyukai