Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Join-pattern wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Object-oriented programming wikipedia , lookup
CAL Actor Language wikipedia , lookup
Name mangling wikipedia , lookup
Go (programming language) wikipedia , lookup
44-141 Computer Programming I Exercise 01 Note that several questions are broken in to two or three parts. Be sure to provide an answer for each italicized part. We are using the Java Programming language in this course. Give the names of at least three other programming languages and a very brief summary (a short paragraph) indicating highlights of the language (for example, what industry or application the language is typically used for, when it was created, etc.). Wikipedia has a “List of Programming Languages” that is an acceptable place to start. Some languages to consider: Cobol, Scheme, Fortran, C, C++, JavaScript, C#, Objective-C, & Lisp. 5 points each: 1 point for the language, 4 points for a clear description of language with a few significant features. 1.The Android operating system, which is available on several smart phones, is based on Java. Take a look at the “Hello World!” program for Android: public class HelloAndroid extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } } (This is an edited version of: http://developer.android.com/guide/tutorials/helloworld.html) In what ways does it resemble the “Hello World!” program we wrote in class? 5 points: Many things are applicable: the keywords (public, class, void), the things in quotes (“Hello...”), the use of punctuation (;), the use of blocks ({}), the use of parenthesis (()), the use of identifiers (HelloWorld, onCreate), the use of camelCase. What is different? 2 points: Many things are applicable. The “extends” is new 2.Create a new class called “Star” and a method named “printStar ()” that will display the following in the Console window. (Hint: if you have trouble, follow the Exercise #1 guide. Use “Star” in place of “HelloWorld” on step 6 and “printStar()” in place of “hello()” on step 12): ************ * * * * * ************ * * * * * ************ Give the code for your program here: public class Star { static void printStar() { System.out.println(“************”); System.out.println(“* * * * *”); System.out.println(“************”); System.out.println(“* * * * *”); System.out.println(“************”); } } 3.Which of the following are valid identifiers: a. Abc b. 2a c. a2 d. _a2_ e. _ f. $_$ g. sum h. 2sum i. studentName j. student’sName k. grossIncome List the valid identifiers here: 1/2 point each incorrect choice Abc, a2, _a2_, _, $_$, sum, studentName, grossIncome 4.Of the “valid” identifiers above, which demonstrate a good style (as discussed in class). Please list the names (not the letters): 1 point each incorrect choice Good style uses descriptive names, camelCase, and avoids the _ and $. sum, studentName, grossIncome 5.Provide an example of the type of data that might be stored with each data type. a. int – Example: 1; 0; -1; 44,123,147; -123 b. double – Example: -2.31, 3.1415, 123.456 6.Create a new class called “IntegerTestOne” and a method named “wrapTest()” that will: a. b. c. d. e. f. g. h. i. j. Create an “int” variable Assign a value of 2,147,483,646 to it Add one to it. Print the value Add one to it. Print the value Add one to it Print the value Add one to it Print the value What is the result of each “print”. List them in order: The numbers (1 point each, being “off by one” is -1) 2147483647 -2147483648 -2147483647 -2147483646 The program (not required): public class NumberTest { static void numberTest() { int theNumber = 2147483646; // Add one to it: theNumber = theNumber + 1; System.out.println(theNumber); theNumber = theNumber + 1; System.out.println(theNumber); theNumber = theNumber + 1; System.out.println(theNumber); theNumber = theNumber + 1; System.out.println(theNumber); } } 7.Create a new class called “RealTest” and a method named “test()” that will: a. Ask the user for a real number (use the “double” type) b. Ask the user for another real number (use the “double” type) c. Print the product of the numbers d. Print the quotient of the two numbers (first number (numerator) divided by second number (denominator)) One run may look like (the user’s input is shown in red): Enter a real number (double): 10 Enter another real number (double): 3 The product is: 30.0 The quotient is: 3.3333333333333335 Provide the code for your program here: import java.util.*; public class RealTest { static void test() { double firstNumber; double secondNumber; Scanner input = new Scanner(System.in); System.out.println("Enter a real number (double): "); firstNumber = input.nextDouble(); System.out.println("Enter another real number (double): "); secondNumber = input.nextDouble(); System.out.println("The product is: "); System.out.println(firstNumber*secondNumber); System.out.println("The quotient is: "); System.out.println(firstNumber/secondNumber); } 12 points: Appropriate prompts in order (3 points), use of Scanner for input (2 points), computing product (2 points), computing quotient (2 points), use of doubles 3 points. } What do you think will happen when you enter 5 for the first number and 0 for the second (There is no wrong answer here. Provide some answer or guess before actually trying it)? 1 point - any guess What DOES happen? The product is: 0.0 The quotient is: Infinity 2 points for the output values. NOTE: “Infinity” is a special value for the double numbers to indicate an infinite number. (Remember that as the denominator in a fraction gets smaller and smaller (like 1/0.1, 1/0.01, 1/0.001, ...) the value goes up and up and up) 8.Create a new class called “IntegerTest” and a method named “test()” that: a. Ask the user for an Integer b. Ask the user for another Integer c. Print out the integer quotient of the first divided by the second d. Print out the remainder of the first divided by the second e. Print out the real number value of the first divided by the second One run may look like (the user’s input is shown in red): Enter a whole number (integer): 10 Enter another whole number (integer): 3 The integer quotient is: 3 The remainder is: 1 The real quotient is: 3.3333333333333335 Provide the code for your program here: import java.util.*; public class InetegerTest { static void test() { int firstNumber; int secondNumber; Scanner input = new Scanner(System.in); System.out.println("Enter a whole number (integer): "); firstNumber = input.nextInt(); System.out.println("Enter another whole number (integer): "); secondNumber = input.nextInt(); System.out.println("The integer quotient is: "); System.out.println(firstNumber/secondNumber); System.out.println("The remainder is: "); System.out.println(firstNumber%secondNumber); System.out.println("The real number quotient is: "); double firstNumberAsAReal = firstNumber; // Note: I'm storing the firstNumber in a double. It will be "widened" to a double. // Then when a double is divided by an int, the int is also widened. // There are other ways to do this: // a) Use multiplication by a double for "widen" the int // 1.0*firstNumber/secondNumber // b) Use type casting (I like this best) // (double)firstNumber/secondNumber System.out.println(firstNumberAsAReal/secondNumber); } } Points: System.out.println for prompts: 4 points Use of scanner (for Ints): 2 points Appropriate use of int variables for input: 2 points Calculations (int division, int remainder, “real” division): 3 points