Chapter 1-3 Flashcards
(27 cards)
How to create an object of a class named “Alternative” = “Instantiate an object of the Alternative class”
Alternative myAlternative = new Alternative();
How do you define a method in Java that doesn’t return a value?
public void methodname(){
//code for method
}
How do you call a method named printMessage() on an object myObject = “invoke the printMessage() method on an instance of the Alternative class”
myObject.printMessage();
how do you define a setter method in java for a string?
public void setCat(String cat){
this.cat = cat;
}
how do you define a getter method for the cat?
public String getCat(){
return cat;
how do you call a setter method setCat(“masha”) on an object myAlternative?
myAlternative.setCat(“Masha”);
how do you call a getter method getCat() on an object myAlternative and print result?
System.out.println(myAlternative.getCat());
declare a private int instance variable for storing the age of a person inside a class
private int age;
why would you write an if else statement to check value of a variable
when you want to execute different blocks of code that depends on whether condition is true or false. helpful for program to handle different scenarios dynamically
how does an if-else statement control program flow
if-else allows program to decide which block of code to run based on condition.
given a user’s input, write an if-else statement to determine whether the user is allowed to access a feature (based on age)
if(age>=18){
System.out. (access granted)
}else{
sys. (access denied)
when would you use else if
when you have more conditions to check to handle multiple outcomes (different grades or categories)
given user input, write if-else statement to print different message for each input
if (input.equals(“yes”)) {
System.out.println(“You chose yes.”);
} else {
System.out.println(“You chose no.”);
}
when might you use nested if-else statement
when you need to check another condition only if the first is true. allows for complex-deciision making where conditions depend on eachother
write nested if-else to check if a number is positive, if positive, check if even or odd
if (number > 0) {
if (number % 2 == 0) { //no numbers left
System.out.println(“The number is positive and even.”);
} else {
System.out.println(“The number is positive and odd.”);
}
} else {
System.out.println(“The number is negative or zero.”);
}
what is basic syntax for a for loop in java
for(initialization ; condition; increment/decrement){
//code to be executed for each iteration
do-while loop syntax:
do{
//code to be executed atleast once
} white(condition);
main difference between a for loop and while loop
for loop is used when you know how many times the loop should run in advance
while loop is when the number is not known, and you need to continue looping based on condition
how would you use a for loop to print num from 1-5
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
how would you use for loop to iterate over an array of numbers and print each val
write a for loop that iterates over an array of integers and prints each element:
int[] numbers = {10,20,30,40,50}
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
what is an array?
collection of elements (values) of the same type. stored in contiguous memory lovations. each element cant be accessed by its index, starting from 0
how do you declare an array of integers with 5 elements in java
int[] numbers = new int[5];
how do you declare and initialize an array with values at the same time?
int[] numbers = {10,20}
how do you access an element in an array
you access using its index. array indices start at 0.
int firstElement = numbers[0];
System.out.println(firstElement);