Card Set Information

2011-02-24 14:10:27
Programming Java

CSC-116: Programming Java
Show Answers:

  1. compiler
    translates our English into 0s and 1s for the processor to understand
  2. source code
    Human written code
  3. byte code
    a class file, which contains 0s and 1s also called machine code
  4. case-sensitive language
    Capital letters and lowercase letters are interpreted differently
  5. public class
    The start of every Java program and contains the code: public class {}
  6. main method
    public static void main(String[ ] args) {}
  7. Code to print to system?
  8. memory
    What a computer uses to store information it needs
  9. variable
    is placeholder for data in a computer program
  10. identifier
    The name of a variable
  11. keyword/reserved word
    Words that are set aside for use in the Java language
  12. int
    Whole Numbers (5, 10, 100,-50)
  13. double
    Decimal Numbers (5.5,10.0583)
  14. char
    A single Character (‘A’,‘b’, ‘&’)
  15. String
  16. assignment operator
  17. initialization of the variable
    Giving a variable an initial value
  18. concatenation operator
  19. How does Java compute concerning the equal sign?
    The right side of the equal sign is computed first.
  20. 5 % 2 calculates what?
    The remainder so the answer would be 1.
  21. Syntax error
    grammatical problem with the source code.
  22. Runtime error
    when your source code compiles, but when you execute it an error occurs
  23. Logic error
    is when the code has correct syntax, causes no errors during execution, but it produces undesired results
    are sections of our source code that are ignored by the compiler
  25. Single Line Comments
    // comment
  26. Area Comments
    /* comment */
  27. Javadoc
    • /**
    • *
    • *
    • */
  28. All _____ files start with public class and the name of the class?
  29. The ______ tells Java where your program should begin execution?
    main method
  30. promotion
    When we divide two numbers where one is an int and the other is a double, the integer value is temporarily changed into a double for the operation.
  31. casting
    • When we want to force Java to temporarily change a value’s type, we can cast the type into something else.
    • (double) 5 // Casts 5 into 5.0
  32. constant
    is a variable whose value can not be changed. Any attempt to do so will cause a syntax error.
  33. how do we declare a variable as a constant?
    • we use the keyword final before the declaration
    • final double PI = 3.14;
  34. methods
    is a group of code that performs a single task
  35. How does a simple method begin?
    public static void ( ) {}
  36. calling or invoking a method means what?
    New methods you write must be told to run from the main method which is invoking or calling the method.
  37. parameters
    When a method needs information to complete its task, it accepts that information through the use of parameters
  38. give an example of a parameter
    public static void method(int x)
  39. argument
    The value you provide in the parenthesis gets stored into the parameter in the method. The value you provide is known as an argument
  40. promotion with integers concerning doubles
    Java will “promote” an integer value into a double value if an integer is given where a double is expected
  41. local variable
    Any variable declared inside a method only exists inside that method
  42. A variable declared inside
    a method is known as one that is ______ to
    that method ?
  43. class variables and instance variables
    kinds of variables that may be used across every method
  44. The Basic Method
    public static void method_name(){}
  45. Methods can send back a value to the location in which the method was called, this is called what?
    returning a value
  46. We write void in the method what does it mean?
    void is written when the method does not return a value
  47. If we want a method to return a value, we replace the word void with the data type that will be returned. What is this called?
    This portion of the method signature is called the method’s return type
  48. The Keyword return
    To return a value, we use the keyword return, followed by the value to be returned.
  49. tags
    contained in a Javadoc and begins with @ and followed by author or version
  50. How would you give someone information about your code without giving them source code?
  51. A example method signature is what?
    public static (params) {….
  52. public static <return_type> <method_name>(params) {} , define the bold terms
    • Where return_type: is the type of value that the method returns. If nothing is returned, we use the keyword void
    • method_name: is the name of your method
    • params: are all the parameters the method uses to accept value
  53. How do you call a method from main ?
    • we simply write the method name followed by parenthesis
    • public static void main(String[ ] args){methodName();}
  54. How can call a method from
    inside another method ?
    • It is no different than calling a method from within the main method.
    • public static void method1(){method2();}
  55. method overloading
    Have two methods in a class that have the same name as long as they accept a unique set of parameters, which allows a user to call a method to calculate the area of a circle and specify their own precision for PI, but also allow them to call it and use a default value of 3.14
  56. When calling overloaded methods if two methods in a class share the same name, the first method has one parameter, the second method has two parameters and we call a the method with only one parameter of type double, which one will excute first and why?
    The method with only one parameter will execute because in method overloading the set of arguments we give determines which method gets called.
  57. Local Variables
    Variables declared inside a method are local to that method. They do not exist in any other method.
  58. Class variables
    are variables that can be used in all methods of the class they are declared and is declared inside the class, but not inside a method
  59. How would you declare a class variable of x = 10 ?
    Immediately following the class line of code you would write: public static int x = 10;
  60. How does Java determine the order for variable use when two variables share the same name ?
    • First look for a local variable with the name we specified, and if it finds it, it will assume we are talking about the local variable
    • If it does not find a local variable, it then looks for a class variable with the name we specified and assume we want to use it
    • If it finds neither; then there is no variable with that name, and Java gives a syntax error
  61. If we have a local and class variable with the same name, Java will use the local variable. What if we actually want Java to use the class variable ?
    We can tell Java to use the class variable by writing the name of the class first, followed by a period, followed by the variable name: Class_name.variable_name
  62. How do you reuse code ?
    All you have to do is write the name of the class that contains the method or class variables that you want to access, and then write a period and the name of what you want to access: Class_Name.method_name() Class_Name.class_variable
  63. Rules of reusing code are ?
    • The class you want to use code from must be in the same directory as the class you are currently writing in order to compile
    • One exception is that you can also compile by setting your class path to know the location of the external code you wish to use
  64. What is each class consist of ?
    class variables and methods
  65. When two classes work together what do they do ?
    they make use of each other’s methods and variables to complete their individual tasks
  66. What happens when a certain class contains a method or a variable that it does not want an outside class to have direct access to?
    The method or variable should be scoped as private
  67. Scope
    refers to the context in which something is visible in programming
  68. Visibility
    refers to whether or not something in our program can be accessed from a given context
  69. public scope
    is the scope we give a class, method, or variable when we want it to visible and thus accessible by all classes everywhere
  70. private scope
    We can keep outside classes from being able to see or access methods and variables in a class by giving them the
  71. protected scope
    The scope allows the given class, method, or variable to only be visible to other classes within the same package as the class they are declared in
  72. package
    In Java, we have the ability to group a set of classes together
  73. What happens if we forget to specify a scope before a method or class variable ?
    Java, by default, gives classes, methods, and variables a default scope of protected
  74. static variables and methods
    belongs only to the class they are associated with
  75. An instance of a class
    The variables used when a class is called without being specified static
  76. instance variables
    variables that no longer belong to the class as objects
  77. An object
    a specific instance of a class
  78. How do you decalre an object ?
    To declare an object, we write the name of the class we are instantiating, then a variable name for the object, followed by a semicolon: Person myself;
  79. How do you create an object ?
    To create an object, we set the variable name equal to the keyword new, followed by the name of the class again with parenthesis after it: Person myself = new Person();
  80. How do you use an object ?
    we can use its instance variables the same way we used to use class variables, with the difference being we specify the object name, and not the class name

    Person myself = new Person(); = “Dan Longo”; myself.age = 21;
  81. What is Boolean logic ?
    logic that evaluates to either true or false
  82. What is a boolean equation ?
    two values with a boolean operator in between
  83. exactly equal
    = =
  84. not equal
    ! =
  85. greater than
  86. less than
  87. greater than or equal to
    > =
  88. less than or equal to
    < =
  89. What is an if statement ?
    When we only want a part of our code to be executed under a certain condition
  90. What is the syntax for an if statement ?
    if ( <condition> ) {// code to be executed}
  91. When is an if statement executed ?
    The only time the code in the {} is executed is when the conditon is true.
  92. How would you declare a primitive variable type boolean ?
    boolean <variable> = true;
  93. Set a boolean variable equal to a boolean equation
    boolean x = 8 > 5; // x is equal to true
  94. Once a boolean variable has a value, it can then be used by itself as the condition for an if statement
    • boolean x = 8 > 5;
    • if( x )
  95. Primitive type boolean used as a parameter type
    public static void example(boolean x)
  96. Primitive type boolean used as a return type
    • public static boolean isLarger(int x, int y)
    • if(x > y)
    • {
    • return true;
    • }
    • return
    • false;
  97. Express how else works in if statements
    • if(d != 0)
    • {
    • denominator = d;
    • }
    • else
    • {
    • System.out.println(“You entered 0”);
    • }
  98. nested if statements
    • if(x > 50)
    • {
    • if(y < 20)
    • {
    • System.out.println(“yes”);
    • }
    • }
    • else
    • {
    • System.out.println(“no”);
    • }
  99. Can you have an if inside an else ?
    • int x = 5;
    • if(x == 10)
    • {
    • System.out.println(“X is 10”);
    • }
    • else
    • {
    • if(x == 5)
    • {
    • System.out.println(“X was 5, not 10”);
    • }
    • }
  100. multiple conditions applied in a single if statement
    and boolean operator: if( x > 10 && x< 20)
  101. When using the and boolean operator what conditions must be true
    False AND False = False

    False AND True = False

    True AND False = False

    True AND True = True
  102. short circuiting
    When the && operator is used, and the first condition evaluates to false, Java does not even look at the second condition:

    if( 5 > 10 && 6 == 6)….
  103. The boolean or operator
    • if(x >5 || x < 0)
    • {
    • // only one of the above need to be
    • // true for this to execute
    • System.out.println(“yes”);
    • }
  104. When using the XOR boolean operator what conditions must be true
    true^ true – evaluates to FALSE

    true^ false – evaluates to TRUE

    false^ true – evaluates to TRUE

    false^ false – evaluates to FALSE
  105. When using the or boolean operator what conditions must be true
    False or False = False

    True or False = True

    False or True = True

    True or True = True
  106. Exclusive OR operator written as XOR
  107. The NOT Operator
    InJava, the NOT operator is represented by the ! symbol, placed directly before a boolean equation

    boolean tf = false;

    • if(!tf)
    • {
    • System.out.println(“TRUE!”);
    • }
  108. switch statement
    • A switch statement is a structure that looks at the value of a variable and goes through a list of possible values
    • Based on the value and the list of possible cases, it will jump to the part of your code where that value is in the list
  109. The possible values in a switch statement list are called ...
  110. switch statement syntax


    case 1:

    case 2:

  111. A value for the case variable that is not in the domain of a switch statement

    • switch(month)
    • {
    • case 1: date = date + 0;
    • break;
    • case 2: date = date + 3;
    • break;
    • default: System.out.println(“no match”);
    • break;
    • }
  112. in a switch statement's case list should have this included
    break; The keyword break tells Java to immediately exit the switch statement
  113. if/else and switch statement
    • A switch statement can be
    • rewritten as an if/else structure

    • An if/else structure (for this specific
    • scenario) can be rewritten as a switch statement
  114. List the cases when converting an if/else to a switch
    When converting an if/else to a switch, each of your if statements would be a case, ending with a break

    If you have an if statement that uses the || (or) operator, you would have two cases together without a break in the middle. There would be a break after both the cases.

    && (and) can not be applied here since a switch only looks at a single variable, which has a single value
  115. give an example of converting an if/else to a switch
    • if/else:
    • if(month == 1)
    • {
    • date = date + 0;
    • }
    • if(month == 4 || month == 7)
    • {
    • date = date + 5;
    • }
    • switch:
    • switch(month)
    • {
    • case 1: date = date + 0;
    • break;
    • case 4:
    • case 7: date = date + 5;
    • break;
    • }