Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSC 207 CSC 207.01 & .02- Algorithms and Object-Oriented Design Spring 2022 Home Schedule Class Slides Labs Assignments Resources Getting Started with Java In this lab, you’ll explore some basics of Java programming using commands in the shell. (We’ll soon switch to the Eclipse IDE.) In the spirit of one of the meta-learning goals of the course, we’ll use these first weeks not just to learn the Java programming language but also the refine our skills at learning new programming languages. The jump from C to Java is not as dramatic as the jump from Racket to C, so we can use this opportunity to develop some best practices for migrating from language to language whether it’s from Java to C#, Java to Python, or to some more exotic languages. You will do a little coding using primative types in Java and learn how Java handles arrays in a manner similar and yet different than in C. Part 1: Building up Your Collection of Programs With the basics of the Java programming process established in the previous lab, you can begin to write more signficant programs. Many programmers break down what they can do in a programming language into two buckets: What they can do with the language itself. What they can do with the language’s libraries. For solving more interesting problems, we’ll need external libraries (either the built-in libraries or some third-party libraries), for example, to perform file I/O or create graphics. But it is worthwhile to tackle the two buckets independently. In particular, learning what primitive operations the language provides gives you insight into how you should model your problems and structure your solutions. Again, Java is a descendant of C, so much of these primitive operations are carried over without any changes. In particular, minus some slightly different syntax and subtly different semantics (which will be exposed as we write more Java): Basic (primitive) types, Variable declarations and assignments, Basic expressions and statements, and Function declarations. are identical between Java and C. Keeping this in mind, try writing a program that solves this problem: Fizzbuzz Write a function fizzbuzz (int n) that takes an integer n and prints the integers from 0 to n (inclusive), one integer per line, using System.out.println for now (there are many ways to format your output that we will learn later!). However, your program should classify each integer using this schema: When n is a multiple of 3, print "fizz", When n is a multiple of 5, print "buzz", and When n is both a multiple of 3 and 5, print "fizzbuzz". You should write this program in a Java file called Fizzbuzz.java. Your main function should demonstrate the results of calling fizzbuzz(100). The fizzbuzz problem is an example of one of the standard programs Peter-Michael Osera tries to write whenever he learns a new language. It’s ideal for this purpose because: It is a short program to write, yet is complex enough to be non-trivial. It tests the language’s expressiveness. In other words, how does one express repetitive and conditional behavior? Part 2: Arrays in Java (2 points) We should also explore some of the more significant, heavyweight features of Java and how they improve over what C provides. One example of this is the array which is a data structure that holds a homogeneous (i.e., same type), fixed-size collection of values. When working with a new type of data, you should always ask yourself the following two questions: How do I create values of this type? How do I use or consume values of this type? Luckily, the array syntax is largely identical to C: The type of an array that holds values of type T in Java is T[], e.g., int[] for an array of integers. To create a new array, we use either an array literal or a new expression passing in the size of the array. For example, the following code snippet creates an array of 5 elements. The first initializes the array: int[] arr1 = { 0, 1, 2, 3, 4 }; // An array literal int[] arr2 = new int[5]; // A "new" expression Array indexing (arr1[0]) works identically to C. Finally, one nice convenience is that Java arrays, unlike C arrays, know their own length: System.out.println(arr2.length); // Prints 5 With all this in mind, try writing some code to answer the following questions regarding initialization of arrays in Java: Perhaps the largest departure from C is that the following code snippet in C int arr[5]; is how you declare an array with five elements in C. Note that there is no array literal or new expression present. What happens if you try this with Java? i.e., declare a variable with an array type, do not use an array literal or new expression to initialize it, and then try to use that array? (New Expressions). Note that the array literal allows you to specify the contents of the array (if you know them at compile time). What value(s) does the new expression use to initialize each element of an array? (Out-of-bounds). Recall that with C arrays, you are free to walk off the end of the array into arbitrary parts of memory (because an array variable is a pointer to the first element)! Can you do this in Java? If not, what error(s) do you get when you try to do this? Do the errors happen at compile time, or runtime? (2 points) Submit your answers to these two questions. Part 3: Program from Scratch (3 points) Finally, try your hand at creating a new program, ABFinder.java that will determine all pairs of positive integers (a, b) such that a < b < 100 and a^2 + b^2 + 1 / (ab) is an integer (i.e. the numerator % denominator == 0). You do not need to worry about elegance at this point. Use the obvious approach!! Note: Java does not contain a square function .... so you will need to write your own public static method. You may not use the Math.pow() method in the Java Math library for this exercise. Don't forget to share the files with your lab partner!! What to Submit You will submit your lab report via Gradescope. Remember, you will need to create one PDF file that includes your answers to part 2 and part 3: Your answers to the questions in part 2 Your program ABFinder.java, which must include your own square method. You may submit one report for both partners, but don't forget to add your partner as an author on the lab report and in the group membership in Gradescope. Acknowledgements This lab is adapted from the Getting Started with Java lab by Sam Rebelsky and exercises in Chapter 1 of our textbook. This derivative work is used and licensed under a Creative Commons Attribution-NonCommercialShareAlike 4.0 International license. This course is adapted from prior courses taught by Shervin Hajiamini, John Stone, Sam Rebelsky, Anya Vostinar, and Jerod Weinman and used by permission. Other authors' contributions are noted when used or adapted.