programming Flashcards

1
Q

How did you design your code? What tools did you use?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How did you know your code was right? How did you test your program?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are methods? What purpose does methods serve?

A

A Java method is a collection of statements that are grouped together to perform an operation/procedure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you declare method?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you call a method?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does method work?

A

The computer reads the data line by line from left to right.

Depends of the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What purpose does methods serve?

A

To perform a specific operation/Procedure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you know when a method returns a value or not? Show me a method in your program?

A

If a method is a void it does not return any value. anything else is a return type that returns a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens when you declare a variable in your program? We may ask you to point to a variable declaration.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Show me some conditional statement you have used in your code? How would you change it from say switch to if else?

A

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");
				}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

Global variable: Declared outside a method, can be used everywhere
Local Variable: Declared inside a method, can be used within the method,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the primitive data type you have used? Show me some.

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A
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);
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What type of errors would I get if I changed this piece of code (e.g take out the semicolon for example).

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Show me code where you take input from the user and how does your program read it?

A
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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does inheritance work in Java? How did you implement it?

A
We implemented in in products. DOBBELT CHECK POLY OG INHERITANCE.
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
17
Q

How does reference variable work? Show me an example from your code.

A

A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

18
Q

What is encapsulation? Show me in your program if you have used it.

A
We have used it in Customer and Employee class
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
To achieve encapsulation in Java
• Declare the variables of a class as private.
• Provide public setter and getter methods to modify and view the variables values.
Benefits of Encapsulation
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.

19
Q

How did you create an object from a class? Show me in your code.

A

We created objects by example:
Class A = new Class.
As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects.
There are three steps when creating an object from a class −
• Declaration − A variable declaration with a variable name with an object type.
• Instantiation − The ‘new’ keyword is used to create the object.
• Initialization − The ‘new’ keyword is followed by a call to a constructor. This call initializes the new object.

20
Q

What is the difference between classes and object? Explain with reference to your code.

A

Object is an instance of a class.
Class is a blueprint or template from which objects are created.
Class is the blueprint which is used to create different objects of the same type.

21
Q

What is the difference between static and non- static modifier in terms of methods and variables? Explain with reference to your code or otherwise.

A

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn’t depend on the individual characteristics of its class, make it static (it will make the program’s footprint smaller). Otherwise, it should be non-static.

22
Q

How does the control of the flow works?

A

The control flow statements in Java allow you to run or skip blocks of code when special conditions are met. You will use control statements a lot in your programs and this tutorial will explain how to do this.

23
Q

What is constuctors, and where do we use it?

A

Firstly a empty constructor is automatically created by java/Ecplice, but is not visible in the class. We use constructors when we create a new object from that class. A constructor needs to have the same name as the class and no return type. Also called when we extends a class.

24
Q

Reference vs Object vs Instance vs Class:

A

Reference vs Object vs Instance vs Class:
A class is basically a blueprint for a house, using the blueprint we can build as many houses as we like based on those classes.
Each house you build (in other words instantiate using the new operator) is an object also known as an instance.
Each house you build has an address (A physical location). In other words if you want to tell someone where you live, you give them your address (perhaps written on a piece of paper). This is known as a reference. Reference can be copied as many times as we like but there is still just one house. In other words we are copying the paper that has the address on it not the house itself.
We can pass references as parameters to constructors and methods.

25
Q

Method overriding:

A

Method overriding means defining a method in a child class that already exist in the parent class with same signature (Same name, same auguments)
We can’t override static methods only instance methods.
Method will be considered overridden if we follow these rules:
• It must have same name and sam arguments.
• Return type can be a subclass of the return type in the parent class
• It cant have a lower access modifier
• For example if the parent method is protected then using private in the child is not allowed but using public in the child would be allowed.
Important points about method overriding to keep in mind:
• Only inherited methods can be overridden, in other words methods can be overridden only in child classes.
• Constructors and private methods cannot be overridden.
• Methods that are final cannot be overrriden
• A subclass can use Super.methodName() to call superclass version of an overridden method.

26
Q

While and do while

A

While and do while
The while loop checks the condition at the start before executing the block.
With the do while loop the code block Is executed at once and then the condition is checked.
We can interrupt the loop by using a continue and/or a break statement.
With the continue keyword the loop will bypass the part of code block that is below the continue keyword and continue with the next iteration.
With the break keyword we can exit the loop depending on the condition that we are checking.

27
Q

What is a reference variable?

A
when you initialize an object, that is a reference variable. e.g. 
Hair myHair = new Hair(); //myHair is a reference variable