首页 > 其他分享 >CSE 8B 计算问题求解

CSE 8B 计算问题求解

时间:2023-04-24 13:58:22浏览次数:36  
标签:code 求解 CSE your will 8B points should method


CSE 8B: Introduction to Programming and
Computational Problem Solving - 2
Assignment 2
Numbers, Mathematics, Characters, and Strings in Java
Due: Wednesday, April 19, 11:59 PM
Learning goals:
● Write Java code that includes:
○ Numbers
○ Mathematical operations
○ String and character parsing
● Write your own test cases to test the correctness of your methods
NOTE: This assignment should be completed INDIVIDUALLY.
Coding Style (10 points)
For this programming assignment, we will be enforcing the CSE 8B Coding Style
Guidelines. These guidelines can also be found on Canvas. Please ensure to have
COMPLETE file headers, class headers, and method headers, to use descriptive variable
names and proper indentation, and to avoid using magic numbers.
Part 0: Getting started with the starter code (0 points)
1. If using a personal computer, then ensure your Java software development environment
does not have any issues. If there are any issues, then review Assignment 1, or come to
the office/lab hours before you start Assignment 2.
2. First, navigate to the cse8b folder that you created in Assignment 1 and create a new
directory named assignment2
3. Download the starter code.
You can download the starter code from Piazza → Resources → Homework →
Assignment2.java
Place the starter code in the assignment2 folder you just created
4. Compile and run the starter code, and you should expect the following output:
Although we have not covered Java methods in lecture yet, you can think of a Java method as a
math function. A method takes in zero or more parameters (input) and returns a value (output).
The starter code is set up so you need not know anything about the details of creating a method
from scratch. Your job is to just fill in the starter code.
Part 1: Implement seven methods (75 points)
In class Assignment2 of the starter code, 9 methods are already declared for you:
circleArea, triangularPrismVolume, minCorrectDivision, concatSubIntegers,
concatAndModify, substringIndex, createStarHourglass, unitTests, and main. For
this part of the assignment, your task is to implement the first seven methods
(circleArea, triangularPrismVolume, minCorrectDivision, concatSubIntegers,
concatAndModify, substringIndex, and createStarHourglass).
Before you implement your methods, please take a look at the constants at the top of the class.
All of the error messages as well as method-specific variables are already defined for you with
the keywords private final static. IMPORTANT: You should use these constants when
developing your methods to ensure that your code will always give the correct output.
You technically do not have to but using them will help you remain consistent.
circleArea (10 points):
This method takes a double variable, radius, as the input which is the radius of a circle, and
the method must calculate the area of the circle with radius radius. Assign your result to the
variable area.
You can assume that the radius will always be greater than or equal to 0.
The formula to calculate the area of a circle is shown below.
Sample:
circleArea(3.0) → 28.27
triangularPrismVolume (10 points):
This method takes two double variables, triangleSideLength and height as input, and
calculates the volume of an equilateral triangular prism with that side length and height. Assign
your result to the variable volume.
IMPORTANT: We are only concerned with equilateral triangular prisms in this
assignment. So triangleSideLength will be the same on all three sides.
The volume of an equilateral triangular prism is shown below.
Sample:
triangularPrismVolume(3.5, 10) → 53.04
minCorrectDivison (15 points):
This method takes in four integer variables a, b, c, and d. You must perform floating point
division between a and b (a/b) and likewise for c and d (c/d). Then you must return the minimum
of these two quantities as a double type. Note that because the variables are passed in as
integers, truncation may happen and yield a wrong result. Hence, you must find a way around
this issue. Calculate the result, and assign it to the variable ans.
Sample:
minCorrectDivision(10, 7, 2, 3) → 0.67
concatSubIntegers (10 points)
This method takes in six variables where x and y represents an integer but in String form:
1. String x
2. int a
3. int b
4. String y
5. int c
6. int d
Your job for this method is to extract the substring from x starting at index a and up to but not
including index b and combine it with the substring from y starting at index c and up to but not
including index d. Then convert this combined result into an integer and assign it to the variable
ans. You may assume that the final answer will not have any leading zeros and that the indexes
are valid for the corresponding strings.
Sample:
concatSubIntegers("56910", 0, 2, "12345", 3, 4) → 564
concatAndModify (15 points):
This method takes two string variables stringOne, and stringTwo as input. The method does
two things:
● combines the two strings
● convert the first and last letter of the combined string into uppercase letters
● it must leave the rest of the characters unchanged
You can assume that the strings x and y will only contain characters from the english alphabet
(26 letters), though the characters can either be lowercase or uppercase. You may also assume
that the strings will not be empty. Assign your answer to the variable ans.
Samples:
concatAndModify("hello", "world") → "HelloworlD"
concatAndModify("Mouse", "Trap") → "MouseTraP"
concatAndModify("Boxed", "milK") → "BoxedmilK"
substringIndex (10 points):
This method takes in three String variables a, b, and c. You must find the first occurrence of c
in b and keep track of the indexes in b where c begins and ends. Then return the corresponding
substring in a with the same start and end indexes. You may assume that c will always be
contained in b at least once and that the start and end indexes will be valid ranges within a.
Extract the result and assign your answer to the variable result.
Sample:
substringIndex("welcometocse8b", "helloworldello", "ello") → "elco"
Explanation:
Because c ("ello") occurs twice in String b ("helloworldello"), we keep track of the first
occurrence which starts at index 1 and ends at index 4. With these start and end indices, we will
extract the corresponding substring within String a ("welcometocse8b") which results in
"elco".
createStarHourglass (5 points):
This method does not take in any inputs. Construct and return a String that uses the asterisk
character '*' to create an hourglass with the number of rows equal to seven.
For example, if we creating an hourglass of height five then the result string should be:
"*****\n *** \n * \n *** \n*****\n" (where \n is the new line character). When this
string is printed out, it should look like this:
NOTE: Notice how each row has one extra space in the beginning and end until you reach the
middle of the hourglass which is just a single '*' which then starts reversing. In this example,
there are 5 rows. In your method, you must construct and return an hourglass whose
height (number of rows) is 7.
Your job is to construct the hourglass and assign it to the variable result.
IMPORTANT (Will lose points if not followed):
1. There must be the right amount of spaces before and after each row. More importantly,
a common mistake is if you forget the spaces after the asterisks and print out the string,
it may look correct in your terminal but will fail the autograder.
2. Append the newline character '\n' after the last space (if any) in every row including
the last row. Do not append anything else. Any extra characters will fail the autograder.
3. As long as your output matches the format of the result string literal shown in quotation
marks above (for a height 9 hourglass), you should pass.
Part 2: Test the correctness of your seven methods (10 points)
Testing is a very important part in programming. In this course, we will get you familiar with unit
testing. For this assignment and all future assignments, you will be asked to create your own
tests to check whether your code works as expected. In this part of the assignment, you
need to implement your own test cases in the method called unitTests.
In the starter code, several test cases are already implemented for you. You can regard it as an
example to implement other cases. The general approach is to come up with different inputs
and manually give the expected output, then call the method with that input and compare the
result with expected output.
You are encouraged to create as many test cases as you think to be necessary to cover all the
edge cases. The unitTests method should return true only when all the test cases are
passed. Otherwise, it should return false. To get full credit for this section, for each of
your seven methods above (except for createStarHourGlass), you should have at least 3
total test cases that cover different situations (including the ones we have provided). In
other words, you will need to create two more tests for circleArea, two more tests for
triangularPrismVolume, two more tests for minCorrectDivision, two more tests for
concatSubIntegers, two more tests for concatAndModify, and two more tests for
substringIndex.
For methods that potentially return a double type which can be very long, you will round the
output to two decimal places by using String.format() then comparing it with the expected
result. An example is shown below and the starting code includes more examples:
An easy way to round the volume to 2 digits of accuracy is to use String.format(). To round
to 2 digits, the format string should be "%.2f". Here is an example of how String.format()
works:
String myStr = String.format("The value is: %.2f", 12.478223);
System.out.println(myStr);
// This will output: "The value is: 12.48"
Part 3: Complete main (5 points)
After completing the three methods and creating several unit tests, compile and run
Assignment2. You should see the message "All unit tests passed". If not, then it is very
likely that you have bugs in your code. Read the previous parts carefully while inspecting your
code to fix your bugs. We call this process debugging.
The main method is the method that will be called when running program Assignment2. Here is
the main method given to you in the starter method:
The code to run unitTests is already given to you. Do NOT change any code that is
provided to you in main. You should only add more lines of code.
First, your program should print the prompt "Please enter your name." (which is already
done in the starter code) and wait for the user to enter feedback.
1. The user will enter their name.
2. You should then print out “Hello [name]! Nice to meet you and welcome to CSE8B.” on a
new line where [name] is the string that the user inputted.
Example: you should be able to exactly reproduce this output below with your program
after the unit testing. If you cannot, then you will lose points on this portion of the
assignment.
Submission
VERY IMPORTANT: Please follow the instructions below carefully and make the exact
submission format.
1. Go to Gradescope via Canvas and click on Assignment 2.
2. Click the DRAG & DROP section and directly select the required file
(Assignment2.java). Drag & drop is fine. Please make sure you don't submit a zip.
Make sure the filename is correct.
3. You can resubmit an unlimited number of times before the due date. Your score will
depend on your final submission, even if your former submissions have a higher score.
4. The autograder is for grading your uploaded files automatically. Make sure your code
can compile on Gradescope.
NOTE: The Gradescope Autograder you see is a minimal autograder. For this
particular assignment, it will only show the compilation results and the results of basic
tests. After the assignment deadline, a thorough Autograder will be used to determine
the final grade of the assignment. Thus, to ensure that you would receive full points
from the thorough Autograder, it is your job to extensively test your code for
correctness via unitTests.
5. Your submission should look like the screenshot below. If you have any questions,
then feel free to post on Piazza!

 WX:codehelp

标签:code,求解,CSE,your,will,8B,points,should,method
From: https://www.cnblogs.com/somtimes/p/17349223.html

相关文章

  • ElasticSearch学习资料
    elasticsearch集群正确关闭、重启方式问题原因:在elasticsearch集群中,当集群发现某个节点关闭时,将延迟一分钟后(默认)再开始将该节点上的分片复制到集群中的其他节点,这可能涉及很多I/O。由于该节点不久将要重新启动,因此该I/O是不必要的。您可以通过在关闭节点之前禁用副本......
  • elasticsearch+filebeat+kafka+kibana——filbeat篇章——overview
    filbeat篇章——overviewhttps://www.elastic.co/guide/en/beats/filebeat/8.7/filebeat-overview.html#filebeat-overview Filebeatisalightweightshipperforforwardingandcentralizinglogdata.Installedasanagentonyourservers,Filebeatmonitorsthelog......
  • codeforces 368B B. Sereja and Suffixes(树状数组)
    题目链接:codeforces368B题目大意:给出一个长度为n的序列a,给出m个查询l,对于每个查询输出[l,n]的区间内不同数的个数。题目分析:将查询按照l的大小排序,从大到小的遍历,每次将>=当前l的位置的a[i]全部加入树状数组,树状数组记录每个数是否出现过。每次结果就是查询树状数组的总和,要保证......
  • elasticsearch——增删改查
    --创建索引PUT/icon_index{"settings":{"analysis":{"analyzer":{"text_anlyzer":{"tokenizer":"ik_max_word","filter":"py"......
  • H3CSE资料
    提供关于SE的学习资料。 点击获取资源,密码:123456......
  • mac elasticsearch tar.gz 离线安装
    1、解压elasticsearch-8.6.2-darwin-x86_64.tar.gz2、需要高版本jdk,大于jdk8,我装的是jdk173、进入解压文件夹bin目录,双击elasticsearch文件,或者bin目录下命令行之行 elasticsearch4、稍微等待,显示 5、浏览器访问https://localhost:9200/ 上图elastic和zqi4S2s80n7bzdgZZ......
  • 最简单一维量子链求解实例
    写在前面:5年前的笔记,再次做个备份.假设器件长度为\(L\),均匀分成\(N+1\)份,网格spacing为\(a=L/(N+1)\).\[H\varphi=-\frac{\hbar^2}{2m}\frac{\partial^2}{\partialx^2}\varphi=E\varphi\]因为\(\varphi(0)=\varphi(N+1)=0\),所以\[\varphi(0)+\var......
  • 已确定迷宫求解所有路线(递归)
    importjava.lang.reflect.Array;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;publicclassMazePath{ publicstaticvoidmain(String[]args){ int[][]maze={{0,1,0,0,0,0}, {0,1,1,0,0,0}, {0,0,......
  • Elasticsearch应用
     vivo互联网的垂直搜索业务刚开始用Elasticsearch时,从0到1的过程没有太大挑战,很快就能实现垂搜的基本功能。但是一旦实现了这个功能之后,搜索引擎需要持续优化迭代,大家会发现诸如分词效果不好,数据召回率或准确率不够等方面的问题。数据索引、检索以及排序这三个方面都有做......
  • elasticsearch 8集群搭建并完成CDN日志收集和解析
     谷歌一下,搜索结果有各种样的博客文章,看了很多篇,反而被绕晕,经过自己的实践搭建完成获取到的经验最适合自己,在这里把整个过程记录下来,也希望可以帮助到一些需要的人。搭建前,请阅读官方文档,虽然是英文,如果能看懂你能学到更精髓的部分。英文好坏也是技术和技术之间的分水岭。好的技......