Java代写:CSE8A Let's Make a Class

代写Java基础作业,练习Class的用法,掌握基本的OOP编程技巧。

Overview

Throughout this course, you have been using and modifying classes that have been predefined for you (World, Turtle, Picture etc.) In this assignment you will be creating a slideshow of images and sounds using 2 brand new classes that you will be creating! This is one of the coolest (and most important) parts about Object-Oriented Programming, and you will have to bring together all the knowledge you’ve accumulated over this quarter to get this done. Let’s get it done!!!

Important Note: This assignment leaves certain details of the solution implementation up to you. This makes the assignment more interesting, as it forces you to be creative about how you decide to implement the solution, and it also tests your complete understanding of all the material presented in this class.

This PSA is worth 20 points total, allocated as follows.

  • Creating new java classes
  • Style and Comments
  • Program Description

As you have to work with a partner for this PSA, please review the guidelines for pair programming. (Remember that you have to work with a partner for each PSA). In addition, note the following details about working with a partner:

  • You must engage in true pair programming as described in the guidelines for pair programming.
  • You must work with the SAME partner for both parts A and B.
  • You and your partner will work on ONE partner’s account, on ONE version of the code.
  • You will submit only ONE submission for the entire PSA8 between the two of you.

How It Should Work

The slideshow should work like this:

  • When we run your program, we should see a series of images (“slides”) appear one at a time, accompanied by a sound. The program should not prompt for input files using the FileChooser, but rather, the files you would like to use can simply be uploaded in the same directory as the source code for convenience, and then hard coded into the program. Please name your picture files as pic1.jpg, pic2.jpg etc. and sound files as sound1.wav, sound2.wav etc.
  • Each image will be accompanied by exactly 1 sound, and will remain displayed for the duration of the sound
  • After the sound is done playing, the currently displayed image will be closed, and the next image will be displayed
  • The slideshow should also randomly flip (either vertically or horizontally) each slide before the slideshow. The probability that the slide will flip vertically vs. horizontally will be controlled by a member variable in the Slideshow class. See the detailed specs below for more information on this.
  • NOTE: You should only use the sound files from the lab’s mediasources folder. Many sounds you download online will have dual channels, which isn’t supported by our Sound class. Additionally, even if the file is single channel, the sample rate will most likely be different and the speed or pitch of the file will sound different when you play it!

Style and Comments

We will be looking for the following additional comment/style points in your code:

  • Meaningful variable names (i.e. don’t name a pixel “n”, “i”, or “bob” or “hello”. Name them something pertinent to their role, such as “sourcePixel” or “targetPixel”)
  • Proper indentation of code. Every subsequent coding/loop block must be further indented properly. Every single code example in your textbook follows this convention, so see your book for examples
  • Comments at beginning of methods indicating what they do at a high level. Example: “//method flips the image upside down”, or “//method mirrors the image along the center vertical line”. You should also include a description of the parameters and return values in the header comments.
  • Inline comments to help you organize your code, as well as aid the graders in understanding your code, especially to give some partial credit
  • Each code line should be no longer than ~80 characters.
  • Each “.java” file should have a file header. The file header should contain the following information of BOTH the partners :
    • Name, login id, Date.

How to Get Started

These instructions assume you are working in the lab machines in B230. We STRONGLY recommend that you work there if you are having any trouble, but if you are comfortable on your own machine, look here
We also assume that you have already set up the programming environment (Dr. Java). If you have not see PSA0 for more directions.

  1. Open a terminal window.
  2. Copy the following lines, paste them on the terminal and then press enter
  3. Verify that no files appear as a result of the previous command
    We haven’t provided you with any files for this assignment! The purpose is to give you a feel for how to create a class from the bottom up, starting with a completely blank file. So hit those Ctrl-N(Command-N in Mac OS) keys in Dr. Java and get ready to make your own class because you’re rolling with the pros now!
  4. Proceed to start your programming assignment.

Creating new java classes

You will be creating two classes in two different Java files, Slide.java and Slideshow.java. First, you should read the description of the classes below. Only begin your assignment AFTER you have read and completely understood the specifications we have given. All methods and member variables should look exactly as how we’ve specified here. If the specifications are not met, or violated in any way, you risk needlessly losing points, which is not Shuaiqi wanting!!

IMPORTANT: Code that does not compile and run will receive very low score on this assignment. It is your responsibility to ensure it compiles and runs properly on lab machines. Most of the testing will be visual and auditory, and just making sure the slideshow plays as expected.

Part A: Slide.java

Write a class named “Slide” in a file called “Slide.java” that represents a single slide in your slideshow.

A.1

The Slide class will have the following member variables (also called instance variables or fields) (0.5 point). You can add these member variables to your class in a similar fashion to how you added “major” to the Student class in Lab 8, so go ahead and review that if you don’t remember how it works.

  • public Picture pic - pic references to a Picture object representing the image to be shown on this slide (0.25 point)
  • public Sound sound - sound references to a Sound object representing the sound to be played while this slide is showing (0.25 point)

A.2

Create the following 2 constructors for the Slide class (remember, it is not necessary to define constructors of every single possible combination of input parameters. We are only requiring that you create the following 2 constructors, but if you want to create more, that’s totally up to you!): (1.5 points)

  • public Slide(): takes no parameters and simply sets pic and sound to null (0.5 point):
  • public Slide(Picture inPic, Sound inSound): takes 2 parameters, inPic and inSound. This constructor denotes that the picture inPic will be displayed when this slide is shown, and will play inSound while inPic is displayed. Note that we are giving you some degree of freedom in terms of how exactly to implement it (e.g., you can either copy the objects, or just reset the reference etc…completely up to you, as long as the end behavior is correct) (1 point)

A.3

You will create mutator methods for your member variables. (2 points)

  • public void changePic(Picture inPic): Changes the picture to be displayed for this slide to the image represented by inPic. This is what we have referred to as a “setter” or “mutator” up until this point, and will be implemented analogously to how the “setMajor()” method was implemented in Lab 8 Part B. (1 point)
  • public void changeSound(Sound inSound): Changes the sound to be played for this slide to the sound represented by inSound. Again, this will be implemented analogously to “setMajor()” in Lab 8 Part B. (1 point)

A.4

And finally, you will create the showSlide method for your class: (1 point)

  • public void showSlide(boolean pictureFlipHorizontal, boolean soundReverse): Shows the modified copy of the slide’s picture based on the parameter pictureFlipHorizontal, and modify the sound based on soundReverse. Then plays the sound, and in the end closes the image. If pic or sound is still null, this method should print an error message, rather than crash the program. It does NOT change the Picture object in the slide object. The boolean parameter, flipHorizontal, is used to indicate whether to flip the copy of the slide’s picture horizontally or vertically before showing it. If flipHorizontal is true, a horizontal flip should be performed on a copy of the calling object’s image. Otherwise, a vertical flip will be performed. You can find more details on horizontal or vertical flips in the hints for B.5 in the next page. Keep in mind that the original picture object of the slide isn’t affected by the showSlide method.

IMPORTANT NOTE: You should thoroughly test your methods in the Slide class before moving on to the Slideshow class below. You are welcome to add a main method to the Slide class for testing purposes, but understand that this is for your own testing only. All of your code that plays the slideshow (which will make calls to methods in the Slide class) will go in the Slideshow.java class, as described below.

Part B: Slideshow.java

Write a class named “Slideshow” in a file called “Slideshow.java” that represents the entire slideshow.

B.1

The Slideshow class will have the following member variables:

  • private Slide[] slides - the array of slides that are in this slideshow. The size of the array must be exactly equal to the number of slides in the slideshow (0.5 point)
    private double probFlip - the probability of flipping the slide horizontally; should be a value between 0 and 1. Note that the probability of flipping the slide vertically is 1 - probFlip.

B.2

Create the following 2 constructors for the Slideshow class:

  • public Slideshow(): takes no parameters and initializes the slides array however you wish to initialize it. Read the addSlide method below for information on how you’ll need to use the slides array.
  • public Slideshow(double inProb): Takes an input parameter for probFlip, and initializes the slides array however you wish to initialize it

B.3

You will create a mutator method for the probFlip member variable

  • public void changeProbFlip(double inProb): Changes the value of probFlip to inProb(input parameter). Note that since this is a probability, it should be between 0 and 1, so you must integrate the proper check to ensure that the input parameter is within that range. If the method is called, you can output some kind of error message like “Inappropriate value for probFlip…probFlip will not be changed”, and simply leave probFlip as it is. For those of you that were asking about the purpose of making a member variable private and only allowing access through designated member methods, this is one example of why you would do it! Don’t wanna let some irresponsible coder set your probability to 18.3 or something do you?

B.4

You will create a method to add a slide to this slideshow: (2 points)

  • public void addSlide(Slide inSlide): Adds a slide, inSlide, to the end of the slides array. Of course, arrays cannot be resized once they are created, so this will require you to make a new array that is one larger than the current size of slides array, and copy the current slides array over, then add the new slide at the end. Don’t forget to update the slides member variable! Hint: This method requires the copying of one array to another, similar to “addGrade()” from Lab 8 Part C. You May Not use an ArrayList or the Arrays Class (1 point)
  • public void changeSlide(Slide inSlide, int slideToChange): Changes the slide at index “slideToChange” to inSlide. This method should properly be able to detect if the index “slideToChange” is within the bounds of the slides array; in the event that slideToChange is not a valid array index, an error method should be printed out, and no slide should be inserted. (1 point)

B.5

You will create a method to show the entire slideshow: (2 points)

  • public void show(): This is the big one!! Shows the entire slideshow from beginning to end. If the slides array is empty, this method should print an error message saying something like “Error! The slideshow is empty!”. Otherwise it should show each slide while playing the corresponding sound (hint: call the showSlide() method appropriately in the Slide class). This method should also be used to decide how to randomly flip your slides or reverse your sound, and pass two boolean variables into the showSlide method to get the correct flip and correct reverse to happen.
    • Hint on possible implementation strategy for actually randomizing the flipping: Remember that the nextDouble() method of the Random class generates a random double between 0 and 1. Therefore, one way to randomly decide if you’re going to flip an image in the slide vertically or horizontally, you can generate a random double using this way for each slide. flip the Picture horizontally. Otherwise, flip it vertically.
    • For this part, you should use the Random class in java built-in library and we want to you hard code the seed..
    • So what is the seed? To be simple, seed is the one to helper you generate the random number. If you want to know more, you could choose CSE 8B next quarter ^ _ ^.
    • To set a random seed in the code, you would replace the code below.

Important Note About Flipping

To flip your Pictures, you should use the flipHorizontal() and flipVertical() methods in your Picture class. You will need to include these methods in your Picture class, as you did in PSA4, and invoke them on Picture objects during the Slideshow. You should NOT re-implement the flipping functionality inside the show() method! We ask that you submit this modified Picture.java (with the two flipping functions) again, just for verification purposes. So, make sure that you submit Picture.java file as well

B.6

And finally, to (partially) test your code and implement the program as described in the “How it Works” section, you will need to create a main method in Slideshow.java to run your slideshow (3 points)

  1. Make at least 4 Slides with 4 different images and 4 different sounds (1 point)
  2. Create a Slideshow using the Slideshow constructor with inProb= 0.6 (0.5 point)
  3. Add the slides to the Slideshow using addSlide() method (1 point)
  4. Display the slideshow using show() method of Slideshow.java (0.5 point)

Important Note About Testing

In this assignment, we are giving you a fair amount of freedom in terms of how to implement your methods, and what error messages will be printed. This is because there are many different ways these classes can be implemented to achieve the desired behavior. As such, testing will be done in two phases.

  • First, we will simply run your submitted main method to see if it plays an appropriate slideshow. For this part, feel free to use any images or sounds that you want for your slideshow (e.g. your modified chromakey images, any other modified images you’ve created throughout this course, any of your edited sounds/palindromes etc.). Please ensure that you upload the picture and the sound files to Vocareum when you submit your assignment!! Make sure you name your picture files as pic1.jpg, pic2.jpg etc. and sound files as sound1.wav, sound2.wav etc.
  • Secondly, we will be creating a tester main method as we did in some of the previous PSA’s that construct our own Slideshow (and also test any potential error message scenarios described in the specifications), and you will be awarded points based on the performance of our tester. So it is to your advantage to do more testing of your methods than your main method in Slideshow will do! For this phase, we will only be using images/sounds from the mediaresources folder.

Program Description

Describe what your program does as if it was intended for a 5 year old or your grandmother. Do not assume your reader is a computer science major. The programs you should comment on in this segment include Slide.java and Slideshow.java. Write this as comments at top of Slide.java file.

Extra Credit [Optional]

You will receive 1 point for submitting CAPE review.
If you submit CAPE reviews for CSE 8A, please write so in the comments at top of Slide.java that you have submitted the CAPE review.

Extra Credit : JUnit Tests [Optional]

The final Extra Credit assignment for this course is not overly difficult, but invaluable to success as a programmer. While running a program and observing its raw output is an effective way to test the correctness of a program on a small scale, as programs get larger (e.g. tens of thousands to hundreds of thousands of lines of code), this “manual” methodology of program testing becomes near impossible, and at the very least, grossly inefficient. There exist ways to write additional “testing code” whose sole purpose is to test a program’s correctness. In Java, JUnit is widely used as a testing framework for this very purpose. Therefore, to earn a Extra Credit for PSA8, you are tasked with writing JUnit tests for the classes you will create.

As this is indeed a Extra Credit, the majority of the “learning” of how JUnit works is left to you. However, the following links should get you started:

How to Turn in Your Homework

Please refer to PSA0 page for instructions to turn in files through Vocareum. The turn in procedure is similar to all previous PSAs, but below is a quick review of the steps.
Important Note: You will not receive full credit if you do not turn in all the required files: Slide.java Slideshow.java, Picture.java, Sound.java (if used). Also submit the picture files (pic1.jpg, pic2.jpg…) and sound files (sound1.wav, sound2.wav…) that you use in your slide show.

  1. Log in to the Vocareum site at http://www.vocareum.com/
  2. Click on the Labs label to the left of your email address in the top right corner.
  3. Click the “Details” link for CSE 8A Fall 2016.
  4. On this web interface, you should see the list of PSA’s assigned up until this point on the left pane. Select PSA8.
  5. You will notice that the “Start” link for the assignment is unavailable until a partner is selected through the system. Use the dropdown menu under the “Partner” pane to search for and select the email address of the partner you will be working with. (Friendly reminder! You can easily find programming partners using Piazza!). After sending the partner request, you must wait then wait for your partner to accept the request before you can click the “Start” link and submit your work.
  6. Once your partner accepts the request, click the “Start” link.
  7. Click on the ‘Upload Assignment’ button to upload your Java files Slide.java Slideshow.java, Picture.java. Also upload the picture files (pic1.jpg, pic2.jpg…) and sound files (sound1.wav, sound2.wav…) that you use in your slide show.
  8. After you are done uploading the java files, the images and the sounds, click on the button ‘Submit Assignment’ to turn in your assignment.
  9. If you make a mistake or want to change anything you can unsubmit and resubmit your files until the deadline. Just don’t forget to submit them before the deadline.
  10. You can view the java files you submitted after clicking the submit button. Use this to verify whether you have indeed uploaded the java files that you intended to upload.
  11. That’s it! We will let you know when your assignment is graded.