uiz #3 will cover Lab #5, Lab #6, Lab #7, and all recent class notes and handout
ID: 3914495 • Letter: U
Question
uiz #3 will cover Lab #5, Lab #6, Lab #7, and all recent class notes and handouts. following topics are especially important to review. The quiz is open I he notes. The differences between class methods and instance methods; Examples of both. 2).Using switch statements &ternary; operators Three main types of loops we've seen: for, while, and do -- their definition, number of times executed 4Writing loops to perform summation, products, counting, and factorial . Sentinel values GCount controlled vs. event controlled loops 7Using break and continue with loops; Using combined assignment operators with loops (e.g., t-,-,-, * , /, and % )-K tc 8. Delay loops; endless loops (how do you get out of?) - BreakExplanation / Answer
Hi please find the answer
If you have any query please let me know first by comment i will give solutions
According to chegg quideline and rule i can give only four subpart answer of question
==================================================
Answer 1-
Differance between class method and instance method
Class Method - The method which is declare with static keyword it is known as class method.For static method you don't need to instantiate the class you can access the methods with the class name . In this method we don't need to create a object
Ex.
public static void methodName()
{
System.out.println("Static Method")
}
// for calling this method
Classname.methodName();
Instance Variable- Methods that are not declared as static are known as instance methods to refer the instance methods we need to instantiate the class first by creating an object of that class.
Ex--
public void methodName()
{
System.out.println("Instance Method")
}
// for calling this method
Classname obj=new Classname();
obj.methodName();
============================================================
Answer - 2
Switch Case Statement
The switch case statement check the value of equality by passing the value into and match the case with this value if found this value then this case statement executed.
Example
Int this program we pass the value day=5 then they will match 5 with case if it found it will execute that case statement.
case 5 = 5 then it will print Friday
// Java program to demonstrate switch case
public class Test
{
public static void main(String[] args)
{
int day = 5;
String dayString;
// switch statement with int data type
switch (day)
{
case 1: dayString = "Monday";
break;
case 2: dayString = "Tuesday";
break;
case 3: dayString = "Wednesday";
break;
case 4: dayString = "Thursday";
break;
case 5: dayString = "Friday";
break;
case 6: dayString = "Saturday";
break;
case 7: dayString = "Sunday";
break;
default: dayString = "Invalid day";
break;
}
System.out.println(dayString);
}
}
=========================
Ternary Operator
The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison.
Example-
result = Statement ? firstvalue : secondvalue;
The first operand in java ternary operator a statement with boolean result. If the first operand is true then ternary operator returns second operand else it returns third operand.
======================================================
Answer 4-
Summation by for loop
Summation means adding of all element upto the n number of value
Example-
public class Loop {
public static void main(String[] args) {
int[] array= {3,4,5,6,3,6,8};
int len=array.length;
int sum=0;
for(int i=0;i<len;i++)
{
sum=sum+array[i];
}
System.out.println(sum);
}
}
Output= Sum of all element is 35
===========================================
Product of element by loop: - calculate the product of all element
public class ProductByLoop {
public static void main(String[] args) {
int[] array= {3,4,5,6};
int len=array.length;
int product=1;
for(int i=0;i<len;i++)
{
product=product*array[i];
}
System.out.println(product);
}
}
Product of element is 360
==========================================
Counting: counting means how many iteration and it is done by while loop
Example-
public class Loop {
public static void main(String[] args) {
int count = 0 ;
int num=5;
while ( count < num )
{
count++ ;
System.out.println(count);
}
}
}
this loop run upto the num=5 means 5 time and its print count value and each loop count is increment by 1;
Output - 1,2 ,3 , 4,5
==================================================
Factorial Number
The factorial of a positive integer n is equal to 1*2*3*...n. and we can calculate here by using the for loop
---------------------------
public class Factorial {
public static void main(String[] args) {
int num=5;
int fact=1;
for(int i=1;i<=5;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}
After executing a program its give. first it loop through upto the number n=5;
Factorial of n 5= 1*2*3*4*5 = 120
Output=120;
==========================================================
Answer -5
Sentinel Values
Sentinel value is a special value that is used to terminate a loop. It is simply any constant that we can choosing and is not a legitimate value in a data set, so you can use it to mark the end of a sequence.
For example, in a loop algorithm that computes non-negative integers, the value "-1" can be set as the sentinel value as the computation will never encounter that value as a legitimate processing output.
It is also refer as flag value or a signal value
Example
public static Grades
{
public class void main (String args[])
{
int next = 0;
int max = 0;
int min = 0;
Scanner keyboard = new Scanner(System.in):
System.out.println("Enter an integer, or -99 to quit: ");
next = keyboard.nextInt();
while (next != -99)
{
if (next > max)
max = next;
else if ( min < max)
min = next;
System.out.println("Enter an integer or -99 to quit: ");
}
System.out.println("Largest: " + max);
System.out.println("Smallest: " + min);
}
}
here -99 is sentinel value it is use here to quite a program;
============================================================
If you have any query please let me know first by comment i will give solutions
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.