Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
BSU CS121 Labs { CS121 } Summer 2018 Toggle navigation Home (current) Schedule Projects Lab Resources CS Tutoring Center Setting up your own machine Program Examples Java 8 API Docs Local Linux Guide MobaXTerm - SSH/SCP Client for Windows Users Video: 5 Tips for Computer Science Students Tutoring Hours Office Hours Toggle navigation Labs Lab Overview 01 Basic Skills Walkthrough 02 Time Conversion 03 Project 1 Startup 04 Color Blob Painter 05 Bouncy Ball 06 Going Loopy 07 ArrayLists and Gradients 08 Shipping Boxes 09 A Game of Chance 10 Word Wrapping 11 Deck of Cards 12 Lite-Brite 13 More GUIs 14 MineWalker GUI Logic 15 Testing and Debugging Pre-lab Homework Answer the following questions on a sheet of paper and bring it with you to your lab section. What is the purpose of the main method in a Java program? What are the steps for creating a directory from the command-line? What are the steps for creating, compiling and running a Java program from the command-line? What is a variable? How do you declare and initialize variable? Given a number of hours, minutes and seconds, what is the mathematical equation you would use to determine the total number of seconds? For example, 1 hour, 30 minutes, and 3 seconds is equal to 5403 seconds. Lab Description You will write two applications that deal with keeping and displaying time in a clock, computer, phone or other device. In computing, time is typically represented as a number of seconds that have passed since a fixed point in the past (such as 1st January, 1970). Since time is represented as a number of seconds, we must convert the number of seconds to hours, minutes and seconds before displaying it to the user. Similarly if a user sets the time in hours, minutes and seconds, we have to convert it to seconds before storing it in the device. Objectives Write a Java application that uses basic data types, variables, and arithmetic expressions Run your first program in Eclipse Part 1: Writing Applications using Variables and Expressions 1.0: Directory Setup Open the command-line console and create a new lab directory called lab02. If you forgot how to do this, look at the Making New Directories section from last lab. Change into your lab02 directory using the cd command. 1.1: Convert hours, minutes, seconds into seconds (PP 2.6) First, you will write a program that converts hours, minutes, and seconds into a total number of seconds. Create a new file named ConvertToSeconds.java using kwrite. In this file, create a class named ConvertToSeconds as shown below. /** * Tell us what your class does. * * @author Your Name */ public class ConvertToSeconds { public static void main(String[] args) { // Your code goes here. } } Complete the class so that it does the following. Declares 3 integer values which represent hours, minutes, and seconds. (You may assume that the values entered are greater than or equal to zero.) Calculates and prints the equivalent number of seconds. Prints the result to the user. Here is a test case with 1 hour, 28 minutes, and 42 seconds. Output: Hours: 1 Minutes: 28 Seconds: 42 Total seconds: 5322 1.2: Convert seconds into hours, minutes and seconds (PP 2.7) Now, you will write another program that does the reverse computation. Create a new file named ConvertToHours.java using kwrite. In this file, create a class named ConvertToHours. Use the ConvertToSeconds as an example to add a main method and comments. Complete the class so it does the following. Declares an integer value which represents a number of seconds. Calculates the equivalent amount of time as a combination of hours, minutes and seconds. Store each value in a separate int variable. You must use the modulus (%) operator in your expressions. You could use subtraction, but we want you to practice using modulo for this exercise so you can familiarize yourself with how it operates. Prints the results to the console. Here is a test case with 5322 seconds. Output: Total seconds: 5322 Hours: 1 Minutes: 28 Seconds: 42 1.3: Convert seconds into hours (fractional) Next, you will add logic to your ConvertToHours program so that it will also convert the given number of seconds to a fractional number of hours. Open your file named ConvertToHours.java using kwrite. Add code to your existing program so it does the following. Calculates the entered number of seconds to a fractional number of hours. Store the result in a double variable called fHours (or whatever name you want to give it. Just keep in mind that you probably already have a variable called hours). Prints the fractional result to the console. The result will show several significant digits. We will learn how to format the output to a smaller number of digits in later. Here is a test case with 5322 seconds. Output: Total seconds: 5322 Hours: 1 Minutes: 28 Seconds: 42 Fractional hours: 1.4783333333333333 1.4: README Create a plain text file in your lab02 directory called README and write your name, class name and section on the top. Follow the format as described in README_TEMPLATE (you may also use the README_EXAMPLE as a reference). Part 2: Introduction to Eclipse Eclipse is an Integrated Development Environment (IDE) for writing, compiling, debugging, and managing programming projects. You can think of an IDE as an all-in-one office suite for programming projects. Importing Existing Programs to Eclipse First, you will import the files you created in Part 1 into Eclipse. Creating a New Java Project in Your Eclipse Workspace. Launch Eclipse from the desktop icon. Select the default workspace and click 'OK'. When Eclipse loads, click the "Workbench" icon in the upper-right hand side of the screen (it can be in the center of the screen in some cases). Navigate to 'File-->New-->Java Project'. Give your project the name: lab02. Select the correct Project Layout (as indicated in the image below) and click on the 'Configure default...' link. Creating a New Java Project Set default to "Project". Click 'OK'. Configure Default Project Layout Click 'Finish'. Importing Files Into Your New Project Right-click on the lab02 project folder in the Project Explorer and select 'Import'. Navigate to 'General-->File System' and click 'Next'. Browse to the directory containing your lab02 source files. Select all source files (recommend checking the whole directory) and click 'Finish'. You should see all your source files in the Package Explorer as shown below. Imported Files in Package Explorer Importing will create a copy of your original lab02 files. If you used the default workspace location when creating your project, you will be able to locate your new files in the console using the command cd ~/eclipse-workspace/lab02 After you verify your files exist in the new location, you may want to remove the old files so you don't get them mixed up. Even though you imported and can run your files in Eclipse, the files are no different than the source code files you created in kwrite. You can still open your files in the new location using kwrite to see that they are just plain text files. Running Programs in Eclipse Now that you have your source files in Eclipse, you can compile and run them in Eclipse. Right-click on your lab02 project folder and select 'Run As-->Java Application'. Or, just click on the green run button in the Eclipse toolbar to run the program that is currently open in the text editor. Running Current Program in Eclipse This action will compile and run your program just like running javac and java from the console. The output of your program will show up in the console at the bottom of the Eclipse window. Submitting the Lab You will follow the same process for submitting each lab. Open a console and navigate to the lab directory containing your source files, Remove all the .class files using the command, rm *.class. In the same directory, execute the submit command for your lab section as shown in the following table. Look for the success message and timestamp. If you don't see a success message and timestamp, make sure the submit command you used is EXACTLY as shown. Required Source Files README ConvertToSeconds.java ConvertToHours.java There will be several hidden files and folders (these will start with a '.' for example, '.classpath') for Eclipse that will show up in your submission. This okay. You want to leave them alone so your Eclipse project will load next time you open it. Section Instructor Submit Command After submitting, you may check your submission using the "check" command. In the example below, replace X with your lab section. Make sure you log out before you leave the lab! The logout option is in the Fedora Linux menu on the main panel at the bottom left. Be sure the login screen is visible before you leave the lab. There are gremlins in the labs who have been known to play tricks on people who forget to log out before leaving.... CS121 is maintained by Marissa Schmidt.