《面向对象程序设计(双语)》课程实验教学大纲 一、课程基本信息 课程代码:22110073 课程名称:面向对象程序设计(双语) 英文名称:Object-Oriented Programming 实验总学时:18 适用专业:计算机科学技术专业 课程类别:专业课 先修课程:程序设计 二、实验教学的总体目的和要求 1、对学生的要求 ()具有C语言程序设计基础: (②)实验课程不仅需要课上投入,课后需要有一定的练习时间: (③)能够根据规范撰写实验报告: 2、对教师的要求 (①)合理设计实验项目: (2)根据实验内容有效控制学生的实验过程: (③)在实验指导过程中,根据学生个性问题提供指导,并且总结共性问题,集中 指导: 3、对实验条件的要求 (1)具有一定性能的电脑和通畅的网络环境: (②)安装JDK和Eclipse
《面向对象程序设计(双语)》课程实验教学大纲 一、课程基本信息 课程代码:22110073 课程名称:面向对象程序设计(双语) 英文名称: Object-Oriented Programming 实验总学时:18 适用专业:计算机科学技术专业 课程类别:专业课 先修课程:程序设计 二、实验教学的总体目的和要求 1、对学生的要求 (1) 具有 C 语言程序设计基础; (2) 实验课程不仅需要课上投入,课后需要有一定的练习时间; (3) 能够根据规范撰写实验报告; 2、对教师的要求 (1) 合理设计实验项目; (2) 根据实验内容有效控制学生的实验过程; (3) 在实验指导过程中,根据学生个性问题提供指导,并且总结共性问题,集中 指导; 3、对实验条件的要求 (1) 具有一定性能的电脑和通畅的网络环境; (2) 安装 JDK 和 Eclipse
三、实验教学内容 实验项目一 实验名称:Introduction 实验内容: 1.Printing Strings Write aJava program thatprints the message,"Roses are red".Your program will be a class definition containinga main method-see the Linco/mexamp in Listing 1.1 ofthe text f youneed guidance.Rememberthe The name of the class must match the name of the file (but without the,java extension) The main method must be inside the class definition(between the first{and the last). The statement that printsthe message must be inside main. .When it works that entire poem Roses are red violets are blue Sugar is sweet And so are you」 2.Documentation File Co Java program that counts English.Frenchand Spanish Save this file to your directory and compile and run it to see what it does.Then modify it as follows: (1)Use /style comments to add a comment header at the top of the file that includes the name of the program,your name,and a brief description of what the program does,neatly formatted (2)Add a comment before each printin that indicates what language the next line is in. Experiment with leaving a blank line before each of these comment lines (in the program itself.not the output).Is the program easier to read with or without these blank lines? (3)Remove one of the slashes from one of your comment lines and recompile the program,so one of the co omments starts with a single/.What you get?Put the slash back in (4)Try putting a comment within acommen so that a/appears after the line.Does this cause problems? (5)Consult the documentation guidelines in Appendix F of the text.Have you violated any of them?List two things that you could imagine yourself or someone else doing in commenting this program that these. 3.Identifiers File contains a simple Java program that prints a message.The identifier that represents the name of this program is Simple but we could have chosen a different identifier subject to certain rules and conventions.An identifier may contain any combination of letters, digits.the underscore character.and the dollar sign.but cannot begin with a digit.Furthermore.by epresent class names (which includes pogram ames)begin with a ta yo wn'tt a Sror if yor name den't rt with
三、实验教学内容 实验项目一 实验名称:Introduction 实验内容: 1. Printing Strings Write a Java program that prints the message, “Roses are red”. Your program will be a class definition containing a main method—see the Lincoln example in Listing 1.1 of the text if you need guidance. Remember the following: The name of the class must match the name of the file (but without the .java extension). The main method must be inside the class definition (between the first { and the last}). The statement that prints the message must be inside main. Compile and run your program. When it works correctly, modify it so that it prints the entire poem: Roses are red Violets are blue Sugar is sweet And so are you! 2. Documentation File Count.java contains a Java program that counts from 1 to 5 in English, French, and Spanish. Save this file to your directory and compile and run it to see what it does. Then modify it as follows: (1) Use // style comments to add a comment header at the top of the file that includes the name of the program, your name,and a brief description of what the program does, neatly formatted. Include a delimiter line (e.g., all stars) at the beginning and end of the header. (2) Add a comment before each println that indicates what language the next line is in. Experiment with leaving a blank line before each of these comment lines (in the program itself, not the output). Is the program easier to read with or without these blank lines? (3) Remove one of the slashes from one of your comment lines and recompile the program, so one of the comments starts with a single /. What error do you get? Put the slash back in. (4) Try putting a comment within a comment, so that a // appears after the initial // on a comment line. Does this cause problems? (5) Consult the documentation guidelines in Appendix F of the text. Have you violated any of them? List two things that you could imagine yourself or someone else doing in commenting this program that these guidelines discourage. 3. Identifiers File Simple.java contains a simple Java program that prints a message. The identifier that represents the name of this program is Simple, but we could have chosen a different identifier subject to certain rules and conventions. An identifier may contain any combination of letters, digits, the underscore character, and the dollar sign, but cannot begin with a digit. Furthermore, by convention, identifiers that represent class names (which includes program names) begin with a capital letter. This means that you won’t get an error if your program name doesn’t start with a
capital letter (as long as what it starts with is legal for an identifier).but it's better style if you do seem arhit rary now but have lots of in your programsyou see the benefit.Of cour,the program name should always be reasonably des riptive of the program. Indicate whether each name below is a legal identifier.and if so.whether it is a good choice to name this program.If the answer to either question is no,explain why.Then save Simple java to your directory and check your answers by modifying it to try each note that you will have to change thefle name each time to match the program name or you will always get an error (1)simple (Why do you even have to change the name of the file in this case?) (2)SimpleProgram (3)1Simple 8 (6)$12345 (7)Simple! 4.Syntax errors yntaxrors in you gives eror m ssages and does no t som of th se messa es are what they mean.Unfortunately,at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred.Your only choice is to carefully study your program to find the error.In the following you will introduce a few typical to a simple program and examine the errormThis i (1)Type the follo the traditional first program a computer scientist writes in a new language. Compile and run the program to see what it does.Then make the changes below.answering the questions as you go (2)Class name different from file name.Delete one (el)from the name of the class (so the first non ent line is public class Helo).save the program,and compile it.What was the error message? (3)Misspelling inside string.Correct the mistake above,then delete one l from the Hello in the message to be printed(inside the quotation marks).Save the program and recompile it There is no error message -why not?Now run the program.What has changed? (4)No ending mark ina string literal.Correct the spelling in the string then delete the ending 9u ation mark enclos ng the ring Hello, Wo Save the program and recompile it.What error message(s)do you get? (5)No beginning quotation mark in a string literal.Put the ending quotation mark back,then take out the beginning one.Save and recompile.How many errors this time?Lots,even though there is really only one emor.When you get lots of errors always concentrate on finding the fixing tha e will fix the rest.After we study variables the error messages that came up this time will make more sense (6)No semicolon after a statement.Fix the last error (put the quotation mark back).Now remove the semicolon at the end of the line that prints the message.Save the program and recompile it.What error mess age(s)do you get?
capital letter (as long as what it starts with is legal for an identifier), but it’s better style if you do. This may seem arbitrary now, but later when you have lots of identifiers in your programs you’ll see the benefit. Of course, the program name should always be reasonably descriptive of the program. Indicate whether each name below is a legal identifier, and if so, whether it is a good choice to name this program. If the answer to either question is no, explain why. Then save Simple.java to your directory and check your answers by modifying it to try each—note that you will have to change the file name each time to match the program name or you will always get an error. (1) simple (Why do you even have to change the name of the file in this case?) (2) SimpleProgram (3) 1 Simple (4) _Simple_ (5) *Simple* (6) $123_45 (7) Simple! 4. Syntax errors When you make syntax errors in your program the compiler gives error messages and does not create the bytecode file. It saves time and frustration to learn what some of these messages are and what they mean. Unfortunately, at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred. Your only choice is to carefully study your program to find the error. In the following you will introduce a few typical errors into a simple program and examine the error messages. (1) Type the following program into a file called Hello.java. (This is the traditional first program a computer scientist writes in a new language.) Compile and run the program to see what it does. Then make the changes below, answering the questions as you go. (2) Class name different from file name. Delete one l (el) from the name of the class (so the first non-comment line is public class Helo), save the program, and recompile it. What was the error message? (3) Misspelling inside string. Correct the mistake above, then delete one l from the Hello in the message to be printed(inside the quotation marks). Save the program and recompile it. There is no error message—why not? Now run the program. What has changed? (4) No ending quotation mark in a string literal. Correct the spelling in the string, then delete the ending quotation mark enclosing the string Hello, World!. Save the program and recompile it. What error message(s) do you get? (5) No beginning quotation mark in a string literal. Put the ending quotation mark back, then take out the beginning one. Save and recompile. How many errors this time? Lots, even though there is really only one error. When you get lots of errors always concentrate on finding the first one listed!! Often fixing that one will fix the rest. After we study variables the error messages that came up this time will make more sense. (6) No semicolon after a statement. Fix the last error (put the quotation mark back). Now remove the semicolon at the end of the line that prints the message. Save the program and recompile it. What error message(s) do you get?
实验性质:验证性 实验学时:2 实验目的与要求 ()掌握输出字符串 (②)熟悉注释方式以及合法的标识符定义: (3)能够识别基本的语法错误; 实验条件: ()CPUi5及以上,内存4G及以上: ②网络顺畅: (3)安装jdk、eclipse或Myeclipse 研究与思考: (1)标识符大小写敏感问题 (2)如何调试错误? 融入点:通过立法监督强调编码的规范性 实验项目二 实验名称:Data and Expressions 实验内容: 1.String literals The goal in this exercise is to develop a program that will print out a list of student names together with other information for each.The ab character (an ape squence)is helpful in getting the (1)Save Names.java to your directory.Compile and run it to see how it works (2)Modify the program so that your name and hometown and the name and hometown of at least two classmates sitting near you in lab also are printed.Save.compile and run the program Make sure the columns line up. ()Modify the program to add a third com with the intended major of each person( Sally's major is Computer Science and Alexander's major is Math).Be sure to add a label a the top of the third column and be sure everything is lined up(use tab characters!). 2.String Concatenation Write a Java program that prints a table with a list of at least 5 students together with their grades earned(lab pints,bonus points,and the total)inthe format below ///////八NN Student Points■
实验性质:验证性 实验学时:2 实验目的与要求: (1) 掌握输出字符串; (2) 熟悉注释方式以及合法的标识符定义; (3) 能够识别基本的语法错误; 实验条件: (1) CPU i5 及以上,内存 4G 及以上; (2) 网络顺畅; (3) 安装 jdk、eclipse 或 Myeclipse 研究与思考: (1) 标识符大小写敏感问题 (2) 如何调试错误? 融入点:通过立法监督强调编码的规范性 实验项目二 实验名称:Data and Expressions 实验内容: 1. String literals The goal in this exercise is to develop a program that will print out a list of student names together with other information for each. The tab character (an escape sequence) is helpful in getting the list to line up nicely. A program with only two names is in the file Names.java. (1) Save Names.java to your directory. Compile and run it to see how it works. (2) Modify the program so that your name and hometown and the name and hometown of at least two classmates sitting near you in lab also are printed. Save, compile and run the program. Make sure the columns line up. (3) Modify the program to add a third column with the intended major of each person (assume Sally's major is Computer Science and Alexander's major is Math). Be sure to add a label at the top of the third column and be sure everything is lined up (use tab characters!). 2. String Concatenation Write a Java program that prints a table with a list of at least 5 students together with their grades earned (lab points, bonus points, and the total) in the format below. ///////////////////\\\\\\\\\\\\\\\\\\\ == Student Points ==
NN\N//1/1/1////1/ Name Lab Bonus Total Joe 43 7 50 william 50 8 58 Mary Sue 39 10 49 The requirements for the programare as follows (1)Print the rder on the top asl rated (using the ss and backsash characters) (2)0S e tab characters to get your columns aligned and you must use the+operator both for addition and string concatenation. (3)Make up your own student names and points-the ones shown are just for illustration purposes.You need 5 names. 3.Escape sequences (1)Observing the Behavior of+To see the behavior of+in different settings do the following a.Study the program below,which is in file PlusTest.java. b.Save PlusTest iava to your directory. c.Compile and run the program.For each of the last three output statements (the ones dealing )write down what was printed.Now for each explain why the computer printed wha If both operands are numbers+is treated as ordinary addition.(NOTE:in the expression a+b the a and b are called the operands If at least one operand is a string the other operand is converted to a string and is the concatenation operator. sinside parent eses are evaluated first.If there are no parentheses the expression is evaluated ef torigh d.The statement about when the computer was invented is too scrunched up.How should that be fixed? (2)Writing Your Own Program with Now write a complete Java program that prints out the following Your program must use only one statement that invokes the println method.It must use the operator both to do arithmetic and string concatenation. 4.Variables Prelab Excercises 5.Arithmetic Expressions The program is supposed to compute the lab grade for a student.To do this it gets as input the number of points the student earned on the prelab assignment and the maximum number of points the student could have earned;the number of points earned on the lab itself and the maximum number of points:the number of points earned on the postlab assignment and the maximum number of points.The lab grade is co das described above:the in-elass and out-of-class grades (in percent)are computed separately then a weighted average of these is computed.The program currently assumes the out-of-class work counts 40%and the in-class counts60%Do the following: (1)First carefully hand trace the program assuming the input stream contains the values 17,20
\\\\\\\\\\\\\\\\\\\/////////////////// Name Lab Bonus Total ---- --- ----- ----- Joe 43 7 50 William 50 8 58 Mary Sue 39 10 49 The requirements for the program are as follows: (1) Print the border on the top as illustrated (using the slash and backslash characters). (2) Use tab characters to get your columns aligned and you must use the + operator both for addition and string concatenation. (3) Make up your own student names and points—the ones shown are just for illustration purposes. You need 5 names. 3. Escape sequences (1) Observing the Behavior of + To see the behavior of + in different settings do the following: a. Study the program below, which is in file PlusTest.java. b. Save PlusTest.java to your directory. c. Compile and run the program. For each of the last three output statements (the ones dealing with 8 plus 5) write down what was printed. Now for each explain why the computer printed what it did given that the following rules are used for +. Write out complete explanations. If both operands are numbers + is treated as ordinary addition. (NOTE: in the expression a + b the a and b are called the operands.) If at least one operand is a string the other operand is converted to a string and + is the concatenation operator. If an expression contains more than one operation expressions inside parentheses are evaluated first. If there are no parentheses the expression is evaluated left to right. d. The statement about when the computer was invented is too scrunched up. How should that be fixed? (2) Writing Your Own Program With + Now write a complete Java program that prints out the following sentence: Ten robins plus 13 canaries is 23 birds. Your program must use only one statement that invokes the println method. It must use the + operator both to do arithmetic and string concatenation. 4. Variables Prelab Excercises 5. Arithmetic Expressions The program LabGrade.java is supposed to compute the lab grade for a student. To do this it gets as input the number of points the student earned on the prelab assignment and the maximum number of points the student could have earned; the number of points earned on the lab itself and the maximum number of points; the number of points earned on the postlab assignment and the maximum number of points. The lab grade is computed as described above: the in-class and out-of-class grades (in percent) are computed separately then a weighted average of these is computed. The program currently assumes the out-of-class work counts 40% and the in-class counts 60%. Do the following: (1) First carefully hand trace the program assuming the input stream contains the values 17, 20
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 -1).In base the maximum u mber 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 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 fle asfollows (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 rogm should random numberbetween(inclusive).It should 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 oints6,17)and(8.10)s8.6023._ot 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 a0 puean后即Pp% ary panel befor adding the e 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 界面实现框架 融入点:谈党和党员的关系,看类和对象的关系 实验项目四
实验名称:Writing Classes 实验内容: 1.Classes and methods (1)File Accountjava containsa parialdefinition for aclass representing a bank account.Save itto your directory and study it to see whatmethods it contains Then complete the Account classas described below.Note that you won't be able to test your methodsuntil you write ManageAccounts in thefor methoretnnt.o number,and balance for the account. b.Fill in the code for method chargeFee.which should deducta service fee from the account. c.Modify chargeFeeso that instead ofretuming void,it retums the new balance.Note that you will have to make changesin two places. d.the code for method changeName which takesa string asa parameterand changesthe name on the accountto be thatstring (2)File ManageAccounts java containsa shell program that uses the Account class above.Save it to your directory,and complete it as indicated by the comments. (3)Modify ManageAccountsso that it prints the balanceafterthecalls to chargeFees.Insteadofusing lce thod eyou aferthedeposthe from variable or embed the method call in a printn statement 2.GUIs:Buttons and TextFields Files voteCounter iava and VoteCounterPanel iava contain slightly modified versions of Pushcounter igva and iava in listings 4 10 and 4 11 of the text as in the text the rom countsthe the bu is a vote for Joe so the button and variableshave been renamed appropriately (1)Compile the program,then run it to see how it works (2)Modify the program so that there are two candidatesto vote for-Joe and Sam.To do this you need to do the following a.Add variablesfor Sam- avote button,and alabel ten for elicks on the button forSam Instantiate an instance of the class when adding the ActionListener to the button for Sam c.Add the button and label for Sam to the panel. (3)Compile and run the program. 实验性质:设计性 实验学时:2 实验目的与要求 (1)掌握设计类的方法: (2)掌握GUI界面中Button,TextField组件的使用:
实验名称:Writing Classes 实验内容: 1. Classes and methods (1) File Account.java contains a partial definition for a class representing a bank account. Save it to your directory and study it to see what methods it contains. Then complete the Account class as described below. Note that you won’t be able to test your methods until you write ManageAccounts in question #2. a. Fill in the code for method toString, which should return a string containing the name, account number, and balance for the account. b. Fill in the code for method chargeFee, which should deduct a service fee from the account. c. Modify chargeFee so that instead of returning void, it returns the new balance. Note that you will have to make changes in two places. d. Fill in the code for method changeName which takes a string as a parameter and changes the name on the account to be that string. (2) File ManageAccounts.java contains a shell program that uses the Account class above. Save it to your directory, and complete it as indicated by the comments. (3) Modify ManageAccounts so that it prints the balance after the calls to chargeFees. Instead of using the getBalance method like you did after the deposit and withdrawal, use the balance that is returned from the chargeFees method. You can either store it in a variable and then print the value of the variable, or embed the method call in a println statement. 2. GUIs: Buttons and TextFields Files VoteCounter.java and VoteCounterPanel.java contain slightly modified versions of PushCounter.java and PushCounterPanel.java in listings 4.10 and 4.11 of the text. As in the text the program counts the number of times the button is pushed; however, it assumes (“pretends”) each push is a vote for Joe so the button and variables have been renamed appropriately. (1) Compile the program, then run it to see how it works. (2) Modify the program so that there are two candidates to vote for—Joe and Sam. To do this you need to do the following: a. Add variables for Sam—a vote counter, a button, and a label. b. Add a new inner class named SamButtonListener to listen for clicks on the button for Sam. Instantiate an instance of the class when adding the ActionListener to the button for Sam. c. Add the button and label for Sam to the panel. (3) Compile and run the program. 实验性质:设计性 实验学时:2 实验目的与要求: (1) 掌握设计类的方法; (2) 掌握 GUI 界面中 Button,TextField 组件的使用;