Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Applying the loop methods with different variables. Result should be like that:

ID: 3841582 • Letter: A

Question

Applying the loop methods with different variables.

Result should be like that:

For making sure my solution is right only!

Problem Statement The goal of this assignment is fairly simple: Idescribe 4 methods that you need to implement and you implement and test them. This is designed to not take too much time. Assignment Create an Outlab4 project and make a class called Loop. The Loop class with not have any instance variables and the constructor will be empty (no parameters, nothing in the body) In Loop you will make the following 5 methods: 1. loopl(int num). In this method, you will make a for loop that prints out the sum of 1's to get to some value, for all values from one to num. See output for more details about how loopl works 2. loop20int lb, int ub, boolean even). In this method, you will print out the even (if passed true or odd (false) numbers from lb to ub including Ib and ub if they even/odd as directed by the passed boolean parameter. Recall what the operator does in Java. 3. loop3(String s). This method prints out the number of times the characters "cat" appear in the input string. Note: The only methods from the String class you are allowed to use as charAto and length0 (read about them online) 4. loop40. This method simulates flipping a random coin. Flip a coin until it hits "Tails". Everytime it hits "Heads", print the probability of getting as many "Heads" as you have currently Create a Driver and test each loop method thoroughly. You must have the class name and method declarations as I describe above. Your TA may choose to use their own Driver to test your Loop class, so you need to use these names, or else it will not compile for them Hints 1. Ask questions if a method's operation does not make sense. Here is sample output. 2. Be careful what parameters you pass the loops. It is not hard to get stuck in infinite loops. To ill program execution, right click the candycane icon in the lower left hand BlueJ screen. Select "Reset Java Virtual Machine"

Explanation / Answer

import java.util.Random;

public class AllLoops {
  
   public void loop1(int num){
       String s;
       s=Integer.toString(num)+"=1";
      
       for(int i=1;i<=num;i++){
           System.out.println(s);
           s = s+"+1";
       }
      
      
   }
  
   public void loop2(int lb, int ub, boolean even){
       for(int i=lb;i<=ub;i++){
           if(i%2==0){
               if(even)
                   System.out.println(i);
           }
           else{
               if(!even)
                   System.out.println(i);
           }
       }
   }
  
   public void loop3(String s){
      
       int lastIndex = 0;
       int count = 0;

       while(lastIndex != -1){

       lastIndex = s.indexOf("cat",lastIndex);

       if(lastIndex != -1){
       count ++;
       lastIndex += "cat".length();
       }
       }
       System.out.println("cat found "+count+" times");
   }
  
   public void loop4(){
       Random rn = new Random();
       double j=0.5;
       int i = rn.nextInt() % 2;

           while(i!=1){
               System.out.println("Heads, "+j);
               j*=0.5;
               i = rn.nextInt()%2;
           }
           System.out.println("Tails");
       }
      

}

//----------------------------------------------------------------------------------------------------


public class Driver {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       AllLoops l = new AllLoops();
      
       l.loop1(7);
       l.loop2(3, 7, false);
       l.loop3("catkhmjgjkhcat");
       l.loop4();

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote