programming Flashcards
How did you design your code? What tools did you use?
Firstly we wrote down the classes we thought we might use and the funktions we needed. we planned, Develop, , wrap, adjust, review, closure. then we created empty (menu) While/Switch with print out for user choices and scanner input. We then created methods and called them in the “menus“. This allowed us to test our program by running it every time a new method was called.
We started working through the customer shopping process.
We used System.out.prinln for testing and Commented out some specific part to try if stuffed could be change or deleted.
How did you know your code was right? How did you test your program?
We made sure to run the program when we made changes. To ensure it worked.
We used System.out.prinln for testing and Commented out some specific parts or methods to try other stuff out.
What are methods? What purpose does methods serve?
A Java method is a collection of statements that are grouped together to perform an operation/procedure.
How do you declare method?
We declare method
• Modifier e.g public static, private, or just public
• return type e.g void (Do not return) or int (return type)
• Parameters
Example:
modifier returnType nameOfMethod (Parameter) {
// method body }
• modifier − It defines the access type of the method and it is optional to use.
• returnType − Method may return a value.
• nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
• Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
• method body − The method body defines what the method does with the statements.
How do you call a method?
We have used two ways to call a method.
1. Object method: we create an instance of a class and then use . then write the method we want to use. Example:
Class A = new Class
A.method()
2. Class method: (Static method) It applies over a class.
Example: Class.method()
How does method work?
The computer reads the data line by line from left to right.
Depends of the method.
What purpose does methods serve?
To perform a specific operation/Procedure.
How do you know when a method returns a value or not? Show me a method in your program?
If a method is a void it does not return any value. anything else is a return type that returns a value.
What happens when you declare a variable in your program? We may ask you to point to a variable declaration.
When declaring a variable, we allow it to store information. If we declare a variable of type int, we allow it to hold integers e.g. a person’s age.
Show me some conditional statement you have used in your code? How would you change it from say switch to if else?
Examples: If and all loops.
Create a example
switch (UserChoice) { //We using a switch statement. Using the input from
case 1: // If UserChoice is 1, this case will run. //Calls the method "customerMenuProcess" from CustomerView class CW.customerMenuProcess();
break;
case 2: //promts employee to login if (login.loggedInEmployee() == false) //If login is false. Print down below System.out.println("Sorry you weren't able to log in.");
//if login true runs "EmployeeProcess" from EmployeeView else EW.EmployeeProcess(); //Calls method "EmployeeProcess" From Employee view class
break; case 3: //exits shop System.out.println("You have exited the shop."); System.exit(0); break; default: //Default out print if user chooses a number thats not between 1-3 System.out.println("Invalid input, please make sure you enter in correct fortmat. \n"); } // Closes switch
if (UserChoice == 1) { //Calls the method "customerMenuProcess" from CustomerView class CW.customerMenuProcess(); } else if (UserChoice == 2) { //promts employee to login if (login.loggedInEmployee() == false) //If login is false. Print down below System.out.println("Sorry you weren't able to log in.");
//if login true runs "EmployeeProcess" from EmployeeView else EW.EmployeeProcess(); //Calls method "EmployeeProcess" From Employee view class } else if (UserChoice == 3) { System.out.println("You have exited the shop."); System.exit(0); } else { System.out.println("Invalid input, please make sure you enter in correct fortmat. \n"); }
Where is the value of the variable in line ?? What happens if I move the declaration to line ?? (hint: scope of the variable). What is the difference between a local and a global variable.
Global variable: Declared outside a method, can be used everywhere
Local Variable: Declared inside a method, can be used within the method,
What are the primitive data type you have used? Show me some.
Primitive data types are predefined types of data, which are supported by the programming language. Used Integer(int), Character (Char), double, Boolean. Others: Float, long, short, byte.
Show me a loop in your code. Now can you change it to a different kind of loop(I will tell you). Show me how you would change it. What are three important aspects of any loops?
We have 3 different kinds of loop, For/ foreach loop, While and Do while. Loops consist of three important parts: The initialization, the condition, and the update. In the initialization step, you set up the variable which you’re going to use in the condition. In the condition step, you perform a test on the variable to see whether you should terminate the loop or execute the body another time. Then, after each successfully completed execution of the loop body, you update your variable. Example of foreach loop: // Print all the array elements for (double element: myList) { System.out.println(element); }
What type of errors would I get if I changed this piece of code (e.g take out the semicolon for example).
There are five types of errors in java( first 3 are errors and last 2 are exceptions ) System errors, syntax errors, semantic errors, Run time errors, Logical errors:
1. System Errors
These type of errors are system or platform related and generally occurs at console. e.g. classpath is not set.
2. Syntax Errors
These types of error occurred due to incorrect grammar in the programming language.
Common examples are:
Misspelled variable and function names
Missing semicolons
Improperly matches parentheses, square brackets, and curly braces
Incorrect format in selection and loop statements
3. Semantic Errors
Semantic errors are compile time errors. The compiler will list the line number and even the word that is causing the issue. e.g. Not declaring an object properly. So if I run into this issue one of the best ways I can resolve this is consult the documentation and find out what the constructor has to say.
4. Runtime errors:
Runtime errors occur during the execution of program. These are also known as runtime exceptions. e.g. Accessing a file which is not present.
5. Logical errors:
Logic errors occur when there is a design flaw in your program. Common examples are:
Multiplying when you should be dividing
Adding when you should be subtracting
Opening and using data from the wrong file
Displaying the wrong message
First three are known as errors in java which cannot be handled during the execution during the program execution.
Last two are exceptions which can be handled using try-catch block.
Show me code where you take input from the user and how does your program read it?
Scanner input = new Scanner(System.in); you make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable input. At the same time you are calling the (so called) constructor of the class, with the parameter System.in. That means it is going to read from the standard input stream of the program. Now when you are calling input.nextInt(); you execute the method from the object you just created (also documented). But as we see, this method returns a integer, so if we want to use that integer, we have to assign the call to a variable like you do: int i = input.nextInt();