23,25.12,15.Trace the program exactly as it is written(it is not comrect but it will compile and the computer would not know it is'orret)Fill in the answers to the following questions a.Show exactly how the computer would execute the assignment statement that computes the out of class average for this set of input Show how the expression will be evaluated (the order in which the operations are performed)and what the result will be. b. Show how the computer would the assignment statement that computes the in-class average.What will the result be c.Show how the computer would execute the assignment statement that computes the lab grade. (2)Now run the program,typing in the input you used in your trace.Compare your answers to the output.Clearly the output is Correct the program.This involves writing the expressions to do calculat .The for the given input should bea out of class average of 82.857(the student earned 29 points out of a possible 35 which is approximately 82.857%),an in-class average of 92 (23 points out of 25),and a lab grade of 88.34(40%of82.857plus60%of92). ()Modify the program to make the weights for the tw ents of the grade variable rather 0.4and .To do this,you d to do fou rather than constants.Note that you should also change their names from all capital letters (the convention for constants)to lowerease letters with capitals starting new words (the convention for variables).So IN WE/GHT should become inWeight Of course ullalso have tochange it whereitsused in the pro dd st tatements that will prompt for the eight (indecimal form-for example.4 for 40%)to be assigned to the in-class work,then read the input Note that your prompt should explain to the user that the weight is expected to be in decimal form. c.In the section that calculates the labGrade add an assigr ent statement that calculate the igh t to be assigned to the out of cass work (this will be ninus the in-class weight) Compile and run your program to make sure it is correct. 6.Input using the Scanner class In this exerise you will use this algorithm toritea program that converts a base 10number toa 4-digit number nother base (yo don't knowenou programming yet to be able size number).The base 10 number and the new base(between 2 and 9)will be input to the program.The start of the program is in the file BaseConert.java.Save this file to you directory,then modify it one step at a time as follows (1)The program will only work correctly for base 10 numbers that fit in 4 digits in the new base quals 15 in buse 10().In base the maximum number is7 which equals 4095 in base 10 (or 84-1).In general,the maximum base 10 number that fits in 4 base b digits is b -1.Add an assignment statement to the program to compute this value for the base that is input and assign it to the variable maxNumber.Add a statement that prints out the result (appropriately labeled).Compile and run the program to makesure it far
23, 25, 12, 15. Trace the program exactly as it is written (it is not correct but it will compile and run so the computer would not know it isn't correct). Fill in the answers to the following questions: a. Show exactly how the computer would execute the assignment statement that computes the out of class average for this set of input. Show how the expression will be evaluated (the order in which the operations are performed) and what the result will be. b. Show how the computer would execute the assignment statement that computes the in-class average. What will the result be? c. Show how the computer would execute the assignment statement that computes the lab grade. (2) Now run the program, typing in the input you used in your trace. Compare your answers to the output. Clearly the output is incorrect! Correct the program. This involves writing the expressions to do calculations correctly. The correct answers for the given input should be an out of class average of 82.857 (the student earned 29 points out of a possible 35 which is approximately 82.857%), an in-class average of 92 (23 points out of 25), and a lab grade of 88.34 (40% of 82.857 plus 60% of 92). (3) Modify the program to make the weights for the two components of the grade variable rather than the constants 0.4 and 0.6. To do this, you need to do four things: a. Change the declarations so the weights (IN_WEIGHT and OUT WEIGHT) are variables rather than constants. Note that you should also change their names from all capital letters (the convention for constants) to lowercase letters with capitals starting new words (the convention for variables). So IN_WEIGHT should become inWeight. Of course, you'll also have to change it where it's used in the program. b. In the input section, add statements that will prompt the user for the weight (in decimal form—for example .4 for 40%) to be assigned to the in-class work, then read the input. Note that your prompt should explain to the user that the weight is expected to be in decimal form. c. In the section that calculates the labGrade add an assignment statement that calculates the weight to be assigned to the out of class work (this will be 1 minus the in-class weight). Compile and run your program to make sure it is correct. 6. Input using the Scanner class In this exercise you will use this algorithm to write a program that converts a base 10 number to a 4-digit number in another base (you don't know enough programming yet to be able to convert any size number). The base 10 number and the new base(between 2 and 9) will be input to the program. The start of the program is in the file BaseConvert.java. Save this file to your directory, then modify it one step at a time as follows: (1) The program will only work correctly for base 10 numbers that fit in 4 digits in the new base. We know that in base 2 the maximum unsigned integer that will fit in 4 bits is 11112 which equals 15 in base 10 (or 24 – 1). In base 8, the maximum number is 77778 which equals 4095 in base 10 (or 84 – 1). In general, the maximum base 10 number that fits in 4 base b digits is b4 – 1. Add an assignment statement to the program to compute this value for the base that is input and assign it to the variable maxNumber. Add a statement that prints out the result (appropriately labeled). Compile and run the program to make sure it is correct so far
2.Now add the code to do the conversion.The comments below guide you through the 3.Sofar the program does o print ou the anwer.Recalthat theis remainders written in reverse order-note that this requires concatenating the four digits that have been computed.Since they are each integers.if we just add them the computer will perform arithmetic instead of concatenation.So,we will use a variable of type String Note near the top of the program a variable named baseBN has been declared as an object of type String and initiali the progam to concatenate the digits in the ne base to baseBNm and then print the answer.Compile and run your program.Test it using the following values:Enter 2 for the base and 13 for the base 10 number-the program should print 1101 as the base 2 value;enter 8 for the base and 1878 for the number-the program should print 3526 for the base 8 value:enter 3for the base and 50 for the number-the program should print 1212. 实验性质:验证性 实验学时:2 实验目的与要求: (1)掌握字符串的连接,转义符的使用: (2)掌握变量,常量的定义和使用: (3)掌握赋值语句和算术表达式的使用 (4)学习使用Scanner类输入数据: 实验条件: (①)CPUi5及以上,内存4G及以上: (2)网络顺畅: (3)安装jdk、eclipse或Myeclipse 研究与思考: ()String中“+”的使用场录 (②)Scanner如何使用? 融入点:表达式的优先级问题可以引申出始终把人民放在心中最高位置这一观 点。 实验项目三 实验名称:Using Classes and Objects 实验内容: 1.String class
2. Now add the code to do the conversion. The comments below guide you through the calculations—replace them with the appropriate Java statements. 3. So far the program does not print out the answer. Recall that the answer is the sequence of remainders written in reverse order— note that this requires concatenating the four digits that have been computed. Since they are each integers, if we just add them the computer will perform arithmetic instead of concatenation. So, we will use a variable of type String. Note near the top of the program a variable named baseBNum has been declared as an object of type String and initialized to an empty string. Add statements to the program to concatenate the digits in the new base to baseBNum and then print the answer. Compile and run your program. Test it using the following values: Enter 2 for the base and 13 for the base 10 number—the program should print 1101 as the base 2 value; enter 8 for the base and 1878 for the number—the program should print 3526 for the base 8 value; enter 3for the base and 50 for the number—the program should print 1212. 实验性质:验证性 实验学时:2 实验目的与要求: (1) 掌握字符串的连接,转义符的使用; (2) 掌握变量,常量的定义和使用; (3) 掌握赋值语句和算术表达式的使用; (4) 学习使用 Scanner 类输入数据; 实验条件: (1) CPU i5 及以上,内存 4G 及以上; (2) 网络顺畅; (3) 安装 jdk、eclipse 或 Myeclipse 研究与思考: (1) String 中“+”的使用场景; (2) Scanner 如何使用? 融入点:表达式的优先级问题可以引申出始终把人民放在心中最高位置这一观 点。 实验项目三 实验名称:Using Classes and Objects 实验内容: 1. String class
The file String Manips.jav contains this program.Save the file to your directory and compile and run it. Now modify the file asfollow (1)Declare a variable of type String named middle3(put your declaration with the other declarations nearthe topof the program)anduse an assignment statement and the substring method to assign middle3 the substring consisting ofthe middle three charactersofphrase(the characterat the middle index together with the charactertothe ft of that and the one to theiht-use variables, compile,and run to test what you have done so far (2)Add an assignment statement to replace all blank chamcters in switchedPhrase with an asterisk (*) The result should be stored back in switchedPhrase (so switchedPhrase is actually changed).(Do not add anotherprint-place your statement the program so that this new value of will be theone printed in the cunt printn statement.),andrun your program (3)Declare two new varablesciryand stateoftype String Add statementsto the program toprompt the user to enter their hometown-the city and the state.Read in the results using the appropriate Scannerelass method-you will need tohave the user enter city and stateonseparate lines.Then ename(all in uppercase letter ed by the city name(all n ers)fol the name (uppercase).So,if the user enters Lilesville for the city and North Carolina for the state,the program should create and print thestring NORTH CAROLINAlilesvilleNORTH CAROLINA 2.Random class rogam should generate arandom numberbetween 1and(inclusive).It shoul the roll for each die and the totalroll(the sum of the two dice),all appropriately labeled.You must use he Random class.The RandomNumbers program in listing3.2 ofthe text may be helpful. 3.Math class te pr putethedist two points Recall that the distance between the two points(yl)and()is computed by takingthe squar root ofthe quantity (xI-x2)2+(y1-y22.The program already has code to get the two points as inpu You need to add an assignment statement to compute the distance and then a print statementto print it out (appropriately labeled).Test your program using the following data:The distance between the points (3.17)and (8.10)is 86023(lots oredigits printed)the distance between(-339)and(9 15)568.352 4.Wrapper class Write a program IntWrapperthat uses the constantsand methods ofthe Integer class (page 140 for a short list,pages 819-820 fora complete list)to perform the following tasks.Be sure to clearly label your output and test your code for each task before proceding that integer (2)Print the maximum and minimum possible Java integer values.Use the constants in the Integer class that hold these values-don't type in the numbers themselves.Note that these constantsare static(see the description onpage140andthesignature npae819)
The file StringManips.java contains this program. Save the file to your directory and compile and run it. Study the output and make sure you understand the relationship between the code and what is printed. Now modify the file as follows: (1) Declare a variable of type String named middle3 (put your declaration with the other declarations near the top of the program) and use an assignment statement and the substring method to assign middle3 the substring consisting of the middle three characters of phrase (the character at the middle index together with the character to the left of that and the one to the right - use variables, not the literal indices for this particular string). Add a println statement to print out the result. Save, compile, and run to test what you have done so far. (2) Add an assignment statement to replace all blank characters in switchedPhrase with an asterisk (*). The result should be stored back in switchedPhrase (so switchedPhrase is actually changed). (Do not add another print—place your statement in the program so that this new value of switchedPhrase will be the one printed in the current println statement.) Save,compile, and run your program. (3) Declare two new variables city and state of type String. Add statements to the program to prompt the user to enter their hometown—the city and the state. Read in the results using the appropriate Scanner class method - you will need to have the user enter city and state on separate lines. Then using String class methods create and print a new string that consists of the state name (all in uppercase letters) followed by the city name (all in lowercase letters) followed again by the state name (uppercase). So, if the user enters Lilesville for the city and North Carolina for the state, the program should crea te and print the string NORTH CAROLINAlilesvilleNORTH CAROLINA 2. Random class Write a complete Java program that simulates the rolling of a pair of dice. For each die in the pair, the program should generate a random number between 1 and 6 (inclusive). It should print out the result of the roll for each die and the total roll(the sum of the two dice), all appropriately labeled. You must use the Random class. The RandomNumbers program in listing3.2 of the text may be helpful. 3. Math class The file Distance.java contains an incomplete program to compute the distance between two points. Recall that the distance between the two points (x1, y1) and (x2, y2) is computed by taking the square root of the quantity (x1 - x2)2 + (y1 - y2)2. The program already has code to get the two points as input. You need to add an assignment statement to compute the distance and then a print statement to print it out (appropriately labeled). Test your program using the following data: The distance between the points (3, 17) and (8, 10) is 8.6023... (lots more digits printed); the distance between (-33,49) and (-9, -15) is 68.352.… 4. Wrapper class Write a program IntWrapper that uses the constants and methods of the Integer class (page 140 for a short list, pages 819-820 for a complete list) to perform the following tasks. Be sure to clearly label your output and test your code for each task before proceding. (1) Prompt for and read in an integer, then print the binary, octal and hexadecimal representations of that integer. (2) Print the maximum and minimum possible Java integer values. Use the constants in the Integer class that hold these values — don't type in the numbers themselves. Note that these constants are static (see the description on page 140 and the signature on page 819)
(3)Prompt the user to enter two decimalintegers,one per line.Use the nextmethod ofthe Scanner class to readeach ofthemn.(Thenex method retumsa String so youneed tostore the values readn String variables,which may seems)No convert the strings to ints (use the approprate method of the Integer class to do this),add them together,and print the sun 5.Panels The program NestedPanels java is from Listing 3.8 of the text Save the program to your directory and do the following (1)Compile and run the program Experiment with resizing the frame and oberve the effecton (2)Modify the program by adding a third subpanel that is twice as wide,but the same height,as the other two subpanels.Choose your own label and color for the subpanel(the color should not be red,green,or blue).Add the panel to the primary panel after the other two panels. (3)Compile anc run the modified progam.Again,experiment with resizing the frame and berve the the components (4)Now add a statement to the program to set the preferred size of the primary panel to 320 by 260.(What would be the purpose of this?).Compile and run the program to see if anything changed. (⑤)Now add another rs pue and s ary panel befor adding the other panels Compile and run the program.What was the effect of this panel? 实验性质:设计性 实验学时:2 实验目的与要求: (l)掌握String,Math,Random,,Wrapper class的使用: (②)握GUI中的Panel组合实现: 实验条件: ⑧路以上内存6及以上 (3)安装jdk、eclipse或Myeclipse 研究与思考: (1)实例化和方法调用 (②)GU1界面实现框架 融入点:谈党和党员的关系,看类和对象的关系 实验项目四
(3) Prompt the user to enter two decimal integers, one per line. Use the next method of the Scanner class to read each of them in. (The next method returns a String so you need to store the values read in String variables, which may seem strange.) Now convert the strings to ints (use the appropriate method of the Integer class to do this), add them together, and print the sum. 5. Panels The program NestedPanels.java is from Listing 3.8 of the text. Save the program to your directory and do the following: (1) Compile and run the program. Experiment with resizing the frame and observe the effect on the components. (2) Modify the program by adding a third subpanel that is twice as wide, but the same height, as the other two subpanels. Choose your own label and color for the subpanel (the color should not be red, green, or blue). Add the panel to the primary panel after the other two panels. (3) Compile and run the modified program. Again, experiment with resizing the frame and observe the effect on the components. (4) Now add a statement to the program to set the preferred size of the primary panel to 320 by 260. (What would be the purpose of this?). Compile and run the program to see if anything changed. (5) Now add another panel with background color blue and size 320 by 20. Add a "My Panels" label to this panel and then add this panel to the primary panel before adding the other panels. Compile and run the program. What was the effect of this panel? 实验性质:设计性 实验学时:2 实验目的与要求: (1) 掌握 String,Math,Random,Wrapper class 的使用; (2) 掌握 GUI 中的 Panel 组合实现; 实验条件: (1) CPU i5 及以上,内存 4G 及以上; (2) 网络顺畅; (3) 安装 jdk、eclipse 或 Myeclipse 研究与思考: (1) 实例化和方法调用 (2) GUI 界面实现框架 融入点:谈党和党员的关系,看类和对象的关系 实验项目四