编程辅导 C C++ Java Python MIPS Processing 网络家教 在线辅导

编程家教 远程写代码 Debug 讲解答疑 课后答疑 不是中介,本人直接答疑

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

 Week 3 Lab - Java Revisited & Java Documentation

COSC244 & TELE2021 IntroductionThe last two labs provided an opportunity for students who have never used Unix before toget familiar with the environment. We will now assume that you are up to speed and readyget on with some Java programming. You will be asked to write a number of small programsduring this lab. The best way to get better at programming is to write programs; lots of them.Make sure you keep a copy of each program that you write.During the first two labs a sensible directory structure was created to keep your labs in(~/244/01, ~/244/02 etc). From now on you should create these lab directories yourself.2 AssessmentThis lab is worth 0.5%The marks are awarded for the written answers to the preparation questions (worth 0.25%)and completing the programming exercises (worth 0.25%). Unlike the previous two labs, youwill not be awarded marks for merely ‘giving it a go’.The preparation questions should be answered before coming to the lab. They should bestored electronically in a plain text file with a .txt suffix. The questions are denoted by a boldQ followed by a number in this document (for example, Q4). Your preparation questions willbe checked at the beginning of the lab. You may be asked to explain your answers to some ofthe questions.To get checked off on the programming exercises, you need to create working programs. Thedemonstrator will look at the source file, ask you to run the program, and may ask questionsabout the code.For this lab, as well as all subsequent labs, to get full marks, you must get your work checked offduring your assigned lab time. We expect you to spend some time ahead of the lab preparingfor it. Normally you can expect to spend around 2 hours of prep time. However, this lab isfairly simple and may not need much time if you are quite competent at Java.Make sure you get your work signed off by a demonstrator before leaving the lab.3 PurposeThis lab has two purposes. The first purpose is to help get everyone up to speed on Javaprogramming. The second purpose is to get everyone familiar with looking up the details ofclasses and methods in the Java API documentation. In later labs, we will provide you someclasses to assist in developing your programming exercises. You need to be able to read andunderstand the provided classes and their methods, and how to use them.14 PreparationTo prepare for this lab, you need access to a web browser and the Internet.4.1 Input streams and output streamsMany of the Java programs you write will read some data from an InputStream, do someprocessing on it and then send it to an OutputStream. The input could come from one ofmany sources, e.g. a network, a file, a keyboard or some other input device etc. Likewiseoutput could be sent to many destinations, e.g. a network, a file, the screen etc. Let’s beginby studying the System IO streams and some of their methods in the Java API documentation.In a web browser, open the course web page: http://www.cs.otago.ac.nz/cosc244Click on Resources, then click on Java API Documentation. Using the top left window, scrolldown to the java.lang package and click on it. Using the bottom left window, scroll down tothe System class and click on it. In the right window you should see the three IO streamswhich belong to the System class.Q1 What is the type of the data field in (System.in)?Click on the type of the data field in to visit the documentation for that class and scroll downto the method summaries.Q2 What is the name of the method which is used to read single bytes from System.in?Q3 What type of value does it return?Q4 What does it return when there is no more data?Click on the System class in bottom left window to go back to the documentation for thatclass. In the right window click on the type of the "standard output stream"Q5 What is the type of the standard output stream (System.out)?Q6 What is the name of the method which is used to write single bytes to System.out?Q7 What type of value does it take?Q8 Write some code which would read bytes from System.in and write them to System.out.Don’t forget to check for the end of the stream. You should wrap your loop in a try-catchblock. (You will be using this code for lab 6)InputStream in = System.in;OutputStream out = System.out;...24.2 The BufferedReader classWhile data is transferred as streams of bytes, it is often more convenient to deal with largerchunks of data, so most languages have some way of buffering the data. Let’s look at theBufferedReader class documentation. You can find it by clicking on the java.io package in thetop left window and then looking near the top of the classes in the bottom left window.Q9 What method would you use to read a line of text?Q10 What is the return type of that method, and What does it return if there is nothingmore to read?4.3 The Scanner classWe often want to deal with input at even higher level, e.g. reading in a certain number ofwords or a specific set of tokens. It’s also nice to have the error checking handled cleanly. TheScanner class gives us these capabilities. Look at the Scanner class documentation which youwill find in the java.util package.Q11 Write a statement to create a scanner to read input from the keyboard.Q12 What method do you use to find out if a Scanner has any more lines to read?Q13 What method do you use to read a line from a Scanner? What does it return?Q14 What method do you use to read the next token from a Scanner? What method shouldyou always call before calling this method?5 Programming ExercisesAll of the programs below should read from System.in and write to System.out.5.1 Program 1The first programming exercise is to write a simple program which reads bytes from an Input-Stream and writes each byte to an OutputStream.5.2 Program 2Copy your first program to another file. Open it in an editor and change the class name tocorrespond to the new file name. Modify the program to read input one line at a time usinga BufferedReader and print the line in reverse (you could use a StringBuilder to reverse it).5.3 Program 3Write a program which reads input one line at a time using a Scanner and prints it outconverted to upper case.Before leaving the lab, show your programs to a demonstrator.35.4 Optional extension exerciseWrite a program which does the following:• Read input one line at a time using a Scanner.– Read white space separated words from the line using another Scanner.– Print out each word followed by a single space, converting each word with lengthfour to ****.• Print out a newline after each line.

相关推荐