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

The first time it was solved with wrong output!!! I need a programmer to solve i

ID: 3862494 • Letter: T

Question

The first time it was solved with wrong output!!!

I need a programmer to solve it with the exact output

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

Loop class

import java.util.Random;

public class Loop {

   Loop()

   {

      

   }

  

   public static void loop1(int num)

   {

       for(int i=1;i<num;i++)

       {

           System.out.print(i+"=");

           for(int j=1;j<=i;j++)

           {

               if(j!=i)

               System.out.print(1+"+");

               else

                   System.out.print(1);

           }

           System.out.println();

       }

   }

  

   public static void loop2(int lb,int ub,boolean even)

   {

       for(int i=lb;i<=ub;i++)

       {

           if(even&&i%2==0)

           {

               System.out.println(i);

           }

           else if(!even&&i%2!=0)

           {

               System.out.println(i);

           }

              

       }

   }

  

   public static void loop3(String s)

   {

       int count=0;

       for(int i=0;i<s.length()-2;i++)

       {

           if(s.charAt(i)=='c'&&s.charAt(i+1)=='a'&&s.charAt(i+2)=='t')

               count++;

       }

       System.out.println("Cat found "+count+" times.");

   }

  

   public static void loop4()

   {

      

       Random rand = new Random();

       int n = rand.nextInt(2)+1;

       String s = (n==1)?"Heads":"Tails";

       int count=1;

       while(s.equals("Heads"))

       {

           System.out.println("Heads,"+Math.pow(0.5,count++));

           n= rand.nextInt(2)+1;

           s = (n==1)?"Heads":"Tails";

          

       }

       System.out.println("Tails");

   }

  

  

}

Driver Class

public class Driver {

   public static void main(String args[])

   {

       Scanner scan = new Scanner(System.in);

       int num = scan.nextInt();

       Loop.loop1(num);       //Test loop1

      

       int lb = scan.nextInt();

       int ub = scan.nextInt();

      

       Loop.loop2(lb,ub,true);       //Test loop2 for even values

       Loop.loop2(lb,ub,false);   //Test loop2 for odd values

      

       String s = scan.nextLine();

       Loop.loop3(s);               //Test loop3

      

       Loop.loop4();           //Test 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