# Register now to access 7 million high quality study materials (What's Course Hero?) Course Hero is the premier provider of high quality online educational resources. With millions of study documents, online tutors, digital flashcards and free courseware, Course Hero is helping students learn more efficiently and effectively. Whether you're interested in exploring new subjects or mastering key topics for your next exam, Course Hero has the tools you need to achieve your goals.

5 Pages

### Homework 2 Code

Course: CS 101L, Winter 2008
School: Kettering
Rating:

Word Count: 677

#### Document Preview

Solve file:///X|/cs101/Homework2_0.txt /* == 3.5 from page 94 == Write a program that generates three single-digit integers and prompt the user to enter the addition of these three integers. */ import java.util.Scanner; public class Homework2_0 { public static void main(String[] args) { // generate the three integers. int num1 = (int) (Math.random() * 10); int num2 = (int) (Math.random() * 10); int num3 = (int)...

Register Now

#### Unformatted Document Excerpt

Coursehero >> Michigan >> Kettering >> CS 101L

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Solve file:///X|/cs101/Homework2_0.txt /* == 3.5 from page 94 == Write a program that generates three single-digit integers and prompt the user to enter the addition of these three integers. */ import java.util.Scanner; public class Homework2_0 { public static void main(String[] args) { // generate the three integers. int num1 = (int) (Math.random() * 10); int num2 = (int) (Math.random() * 10); int num3 = (int) (Math.random() * 10); // prompt the user. System.out.print(num1 + " + " + num2 + " + " + num3 + " = "); Scanner input = new Scanner(System.in); if (input.nextInt() == num1 + num2 + num3) { System.out.print("Correct!"); } else { System.out.print("Incorrect! The answer was " + (num1 + num2 + num3) + "."); } // no errors. System.exit(0); } } file:///X|/cs101/Homework2_0.txt [4/29/2008 12:10:00 PM] file:///X|/cs101/Homework2_1.txt /* == Solve 3.8 from page 94 == Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables num1, num2, and num3, respectively. The program sorts the numbers os that num1 <= num2 <= num3. */ import javax.swing.JOptionPane; public class Homework2_1 { public static void main(String[] args) { // prompt the user for the integers. int num1 = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the first integer:", "Homework2_1 Input",JOptionPane.QUESTION_MESSAGE)); int num2 = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the second integer:", "Homework2_1 Input",JOptionPane.QUESTION_MESSAGE)); int num3 = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the third integer:", "Homework2_1 Input",JOptionPane.QUESTION_MESSAGE)); // use a stacked bubble sort. int swap; while (num1 > num2) { swap = num2; num2 = num1; num1 = swap; if (num2 > num3) { swap = num3; num3 = num2; num2 = swap; } } // print the result. JOptionPane.showMessageDialog(null,num1 + " <= " + num2 + " <= " + num3); // no errors. System.exit(0); } } file:///X|/cs101/Homework2_1.txt [4/29/2008 12:10:00 PM] file:///X|/cs101/Homework2_2.txt /* == Solve 3.9 from page 94 == Write a program that reads three edges for a triangle and computes the perimeter if the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of any two edges is greater than the third edge. */ import java.util.Scanner; public class Homework2_2 { public static void main(String[] args) { // prompt the user for the edge lengths. Scanner input = new Scanner(System.in); System.out.print("Enter the first edge length: "); int edge1 = input.nextInt(); the System.out.print("Enter second edge length: "); int edge2 = input.nextInt(); System.out.print("Enter the third edge length: "); int edge3 = input.nextInt(); // test the input for validity. if (!((edge1 + edge2 > edge3) && (edge1 + edge3 > edge2) && (edge2 + edge3 > edge1))) { System.out.print("Invalid input! The sum of any two edges must be great than the third."); System.exit(1); } // compute and display the perimeter. System.out.print("The perimeter of a " + edge1 + "-" + edge2 + "-" + edge3 + " triangle is " + (edge1 + edge2 + edge3) + "."); // no errors. System.exit(0); } } file:///X|/cs101/Homework2_2.txt [4/29/2008 12:10:00 PM] file:///X|/cs101/Homework2_3.txt /* == Solve 3.11 from page 94 == Write a program that prompts the user to enter the month and year, and display the number of days in the month. For example, if the user enter month 2 and year 2000, the program should display that February has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days. */ import java.util.Scanner; public class Homework2_3 { public static void main(String[] args) { // set up the array of month strings. String[] months = {"January","February","March","April","May","June","July","August","Septemeber","October","November","December"}; // prompt the user for a month and year. Scanner input = new Scanner(System.in); System.out.print("Enter a month, where 1 = January and 12 = December: "); int month = input.nextInt(); System.out.print("Enter a four-digit year: "); int year = input.nextInt(); // break the logic into months. int days = 0; switch(month) { case 1: // January case 3: // March case 5: // May case 7: // July case 8: // August case 10: // October case 12: // December days = 31; break; case 4: // April case 6: // July case 9: // September case 11: // November file:///X|/cs101/Homework2_3.txt (1 of 2) [4/29/2008 12:10:01 PM] file:///X|/cs101/Homework2_3.txt days = 30; break; case 2: // February if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) { days = 29; } else { days = 28; } break; default: // non-existant month. System.out.print(month + " does not correspond to a valid month in the Gregorian calender."); System.exit(1); } // output the result string. System.out.print(months[month-1] + " " + year + " has " + days + " days."); // no errors. System.exit(0); } } file:///X|/cs101/Homework2_3.txt (2 of 2) [4/29/2008 12:10:01 PM]
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Kettering - CS - 101L
CS101 Lab3 Objectives Loop control and selection control statements Java Input and output Numerical computation Documentation and formatingLab descriptionIn this lab, you are going to develop a program that can compares loans with various i
Kettering - CS - 101L
file:/X|/cs101/Homework7.txt/* 3 functions: open new account, perform account transaction, quit. */ import java.util.Scanner; public class Homework7 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String buffer; d
Kettering - CS - 101L
file:/X|/cs101/Lincoln.txtpublic class Lincoln { /-/ Prints a presidential quote. /-public static void main (String[] args) { System.out.println (&quot;A quote by Abraham Lincoln:&quot;); System.out.println (&quot;Whatever you are, be a good one.&quot;); } }file:/X|
Kettering - CS - 101L
Lab 5 for CS101 Objective: Arrays This Java lab project addresses the ability to work with single and multidimensional arrays in Java. In this lab project, we consider how we can create arrays, index elements of arrays, set elements of arrays, pass
Kettering - CS - 101L
Objective: Lab 7 class definition and object creation UML diagram Write multiclass Java programLab descriptionIn this lab, you are going to design a class named Stock that contains A string data field named symbol for the stock's symbol
Kettering - CS - 101L
Lab 7Objective File I/O, Scanner, PrintWriter Class and Objects Array object array sorting Lab description In this lab, you are going to write a program that read in from a text file about the information of a bunch of students. For each stu
Kettering - MECH - 210
Chapter One: Basic Concepts2Irwin, Basic Engineering Circuit Analysis, 8/EChapter One: Basic Concepts3SOLUTION:4Irwin, Basic Engineering Circuit Analysis, 8/ESOLUTION:Chapter One: Basic Concepts5SOLUTION:6Irwin, Basic Engine
Kettering - PHYS - 114
Page 2 of 6PHYS-114Test #1Summer 2006[1] (10 pts) A car driving South slows down slightly as it approaches a curve in the road ahead. The car rounds the curve at constant speed and then speeds up slightly as it heads off East. Draw a complete
Kettering - CS - 101L
file:/X|/cs101/Homework6_0.txt/*[6.5 from p204] Write a program that reads in ten numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once).*/ import java.util.Scanner; public class Homework6_0
Kettering - CS - 101L
Lab 8 CS101 Fall 2007 Objective File I/O Class definition and object creation Object array Inheritance and polymorphism Lab description In this lab, you are going to write a program that read a set of geometry objects from a text file. The ge
Kettering - PHYS - 114
Winter 2008PHYS-114Test #1Page 1 of 6PHYS-114, Newtonian Mechanics Test #1 (Chapter 1,2,3,4)Rules:[1] This exam has 6 pages. There are five shorter questions and one long problem. [2] You must show ALL work in order to receive complete cred
Kettering - PHYS - 114
PHYS-114 Test #3Chapters 9,10,11(Conservation of Momentum and Energy) Name (please print): _ Raw Score: _ Test Grade: _[1] (10 pts) A 3.7kg box is sliding across an icy pond with a velocity of 8.0 m/s. A gust of wind exerts a force as shown in
Kettering - PHYS - 114
PYYS-114Practice Test #2Page 1 of 6[1] A 1.0-kg physics book is given an initial velocity of 3.0 m/s up a ramp. The ramp makes an angle of 20 above the horizontal. If the coefficient of kinetic friction between the book and the surface is 0.2,
Kettering - PHYS - 114
PHYS-114 Test #3Chapters 9,10,11Winter 2008(Conservation of Momentum and Energy) Name (please print): _ Raw Score: _ Test Grade: _[1] (8 pts) In an episode of Futurama Professor Farnsworth uses his Smell-O-Scope to detect a gigantic 5 ball of
Kettering - CHEM - 136
Kettering - PHYS - 114
Winter 2008PHYS-114Test #2Page 1 of 7PHYS-114, Newtonian Mechanics Test #2 (Chapter 5,6,7,8)Rules:[1] This test has 4 worked out problems with plenty of room for your work. [2] You must show ALL work in order to receive complete credit for
Hofstra - ENGG - 10
THRUA2537 7 R2156x6x27B128APROPRIETARY AND CONFIDENTIAL THE INFORMATION CONTAINED IN THIS DRAWING IS THE SOLE PROPERTY OF &lt;INSERT COMPANY NAME HERE&gt;. ANY REPRODUCTION IN PART OR AS A WHOLE WITHOUT THE WRITTEN PERMISSION OF &lt;INSER
Cornell - MPS/ILR/NY - OB 525
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46Organizational Behavior (OB): The Action Perspective ILROB 525; Spring 2008 Sam Bacharach Notes by Christine Early Class
University of Florida - PHY - 2048
Ch 4 notes3d-motion: general equations of motion Projectile motion (special case)^ ^ ^ 3d-position: r = xi + yj + zkyrr1r2x^ ^ r1 = x1i + y1 ^ + z1k j ^ ^ r2 = x2i + y2 ^ + z2 k j ^ ^ ^ r = xi + yj + zk ^ ^ r (t ) = x(t )i + y (t ) ^ + z (
University of Florida - PHY - 2048
Ch3: VectorsScalar: just a number mass (m) time (t) pressure (P) temperature (T) energy (E) . Vector: magnitude + direction displacement r velocity v accelerationa force F momentum p .Adding Vectorsc = a +bbac acbTo add vec
University of Florida - PHY - 2048
1-d Motion: Position &amp; Displacement The x-axis:We locate objects by specifying their position along an axis (in this case x-axis). The positive direction of an axis is in the direction of increasing numbers. The opposite is the negative direction.
University of Florida - PHY - 2048
Force &amp; Motion: Friction Coefficient of static friction s:Case 1: v = 0 &amp; a = 0FN Block M FextIf on object does not move (i.e. v = 0 a = 0) then the static frictional force, fs, and the component of the external force Fext that is parallel to th
University of Florida - PHY - 2048
Force &amp; Motion: Newton's Laws (1st Law) If no net force acts on a body then the body's velocity cannot change. Inertial mass!Zero net force implies zero acceleration. (2nd Law) The net force on a body is equal to its mass r all r times its accele
Texas A&M - PHYS - 222
PHYSICS 222 EXAM C April 17, 2008N AME (PLEASE PRINT)w jJoffJeeqjjatojtsheets nd 1 site your nameor, the hack of the. exam.)wave function (30) 1 consider an electron trapped in an infinite 14e11 with width a 1 he will be 0 for x 0 and x a and to
Texas A&M - PHYS - 222
_____PHYSICS 222 EXAM B a February 28, 2008NAME (PLEASE PRINT)(Tear off the equation sheets and write your name on the back of the exam.) (10) 1. At what peak wavelength does an object at 37 C radiate ?c. 9.4x I0 m 6 d. 7.8 x 10 mT
Texas A&M - MATH - 311
Math 311 midterm exam I answers1. We need to solve the system 1 -1 1 1 t t 1 - s 1 = 1 -1 s 1 0 1 0 3 1 . = 2Using row reduction, 1 1 4 1 1 4 1 0 3 1 1 4 1 -1 2 0 -2 -2 0 1 1 0 1 1 . 1 0 3 0 -1 -1 0 0 0 0 0 0 Thus t =
Texas A&M - ECEN - 325
Texas A&M - ECEN - 214
Texas A&M - PHYS - 222
Texas A&M - ECEN - 214
Texas A&M - MATH - 311
Math 311 second practice examInstructions: This is a closed-book exam. You have 50 minutes. Use the backs of the pages for scratch paper; if you attach more pages, write your name on every extra page you use. Make sure to show all your work, and pro
Texas A&M - MATH - 311
Math 311 first practice examInstructions: This is a closed-book exam. You have 50 minutes. Use the backs of the pages for scratch paper; if you attach more pages, write your name on every extra page you use. Make sure to show all your work, and prov
Texas A&M - PHYS - 222
Texas A&M - PHYS - 222
Texas A&M - ECEN - 325
Texas A&M - PHYS - 222
PHYSICS 222 EXAM A a February 7,2008NAME_ (PLEASE PRINT)(Tear off the equation sheets and write your name on the back of the exam) (10) 1. With respect to the earth, object 1 is moving at speed 0.8c to the right. Object 2 is moving in the same di
Texas A&M - MATH - 311
Math 311 midterm exam II answers1. Since (3, 3) = 2(1, 2) + (1, -1) and the function f is linear, f (3, 3) = 2f (1, 2) + f (1, -1) = 2(1, 2, 3) + (0, -2, 4) = (2, 2, 10). Although the argument above is sufficient, one can also calculate the whole ma
Texas A&M - MATH - 311
Math 311, Homework 6 partial answers and solutions3.2.7. (optional) Subspace, since s x y a b +t z x c a = sx + ta sy + tb . sz + tc sx + ta3.2.8. Answer: subspace. Detailed solution. The subset S is the collection of all matrices of the form x y
Texas A&M - MATH - 311
Math 311, Homework 3 partial answers and solutions2.2D.2. Answer: u -1 -1 1 1 1 v 1 0 0 0 0 w = s1 0 + s2 1 + s3 0 + s4 0 + 0 . x 0 0 1 0 0 y 0 0 0 1 0 2.2D.4. Partial solution: a possible reduced form of thi
Texas A&M - MATH - 311
Math 311, Homework 1 partial answers and solutions1.1.9. We need to solve the system 3a = 9, -a + b = -1, 5b = 10. There is exactly one solution (a = 3, b = 2). 1.1.12. Answer: the only vector with the first coordinate 6 that is a linear combination
Texas A&M - MATH - 311
Math 311, Homework 10 partial answers and solutions3.7B.3. We apply the Gram-Schmidt procedure. Let v1 = (1, 2, 1, 1). Since (1, 2, 1, 1) and (1, 2, 1, 1), (-1, 0, 1, 0) = 0, we can take v2 = (-1, 0, 1, 0). Since (-1, 0, 1, 0) and (-1, 0, 1, 0), (0,
Texas A&M - PHYS - 222
CONSTANTS c = 3 x10 8 m / s e = 1.6 x10 -19 J 1eV = 1.6 x10 -19 J W = 5.67 x10 -8 2 4 m K J k = 1.38 x10 - 23 K -3 b = 2.9 x10 m K h = 6.63 x10 -34 J s h = 4.136 x10-15HYDROGEN ATOMEn = -13.6 eV n2NUCLEARN (t ) = N 0 e - tt1 =2ln 2
Texas A&M - MATH - 311
Math 311, Homework 7 partial answers and solutions3.4.5. f : R3 R2 is a linear function, and has a matrix representation x x y = 1 4 3 y . f 2 5 4 z z So its image is the span Span Since 1 0 and 0 1 = 1 3 4 + - , 2 4 5 = 3 1 -2 4 2 1 4 3 , ,
Texas A&M - MATH - 311
Math 311, Homework 8 partial answers and solutions3.5C.1. (a) Any matrix A with entries aij is a linear combination of the matrices Eij :m nA=i=1 j=1aij Eij ,so these matrices span all of Mm,n . Conversely, any linear combinationm naij Ei
Texas A&M - MATH - 311
Math 311, Homework 9 partial answers and solutions3.6C.2. Answer: Theorem 6.7 does not apply, no basis of eigenvectors. 3.6C.3. The characteristic polynomial det - 1 -1 - = 2 + 1has two distinct complex roots = i. Thus the matrix has a basis of e
Texas A&M - MATH - 311
Math 311, Homework 4 partial answers and solutions2.5.5. -1 0 1 2 -1 0 1 2 1 2 -1 0 0 1 2 -1 1 2 -1 = - det 2 0 2 det = det 0 1 2 0 2 2 -1 0 -1 2 5 0 -1 2 5 2 -1 0 1 1 2 -1 -4 4 = - det 0 -4 4 = - det = -(-16 - 16) = 32. 4 4 0 4 4
Texas A&M - MATH - 311
Math 311, Homework 5 partial answers and solutions3.1.1. 1 2 . 2 4 The columns of A are linearly dependent, and A is not invertible, so f is not one-to-one. A= 3.1.4. Answer: A= Not one-to-one. 3.1.5. 1 0 Thus f (e1 ) = Similarly, f (e2 ) = 3.1.8. A
Texas A&M - MATH - 311
Math 311, Homework 2 partial answers and solutions1.4.8. Answer: (a) 13. (b) 11 and 17. (c) arccos 13 0.26 radians. 181 1.4.15. Since |v| = 1+9+4= 14, the unit vector in the direction v is 1 3 2 , , - 14 14 14 .n=Thus (a) x n = 2-9-2 =
Texas A&M - ECEN - 325
Texas A&M - ECEN - 325
Texas A&M - ECEN - 325
Texas A&M - ECEN - 325