Question

In: Computer Science

How would I get this started: Code a logic program that uses a for loop to...

How would I get this started:

Code a logic program that uses a for loop to call your method 5 times successively on the values 1 through 5. (i.e. it would display the val and it’s square…)

Solutions

Expert Solution

Java code for the given requirement:

public class Demo{

   public static void main(String arg[]){

       //Using for loop to call the method exactly five times for the values of 1 through 5

          for(int i=1; i<=5; i++) {

                 //Calling the display square method

                 displaySquare(i);

          }

     

   }

   private static void displaySquare(int i) {

              //TODO Auto-generated method stub

              //displaying the value and its square

              System.out.println(i+" and it's square is "+i*i);

       }

}

Output:


Related Solutions

How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
How would you represent a for loop in First Order Logic terms? Is it possible to...
How would you represent a for loop in First Order Logic terms? Is it possible to implement a for loop using First Order Logic terms? What about Propositional Logic?
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output the largest of those numbers, you can assume any length for those numbers. 80x86 assembly language
How would I write this driver program in C++?? We just started learning C++ and I...
How would I write this driver program in C++?? We just started learning C++ and I still don't understand how to use classes or randomly generate numbers, and I'm struggling to complete this assignment. The premise of the whole assignment is to randomly generate dates within a range and format the dates in 3 different ways. Calendar.cpp Create a driver program with a main function. Use symbolic constants or "const" declarations here, also. In particular, define local constant SIZE to...
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
I am not quite sure how to get this program working or how to start it...
I am not quite sure how to get this program working or how to start it in the first place. Write a full Java program and do the following: 1- Create a generic class and declare a one dim array as a private member. 2- Add a constructor to initialize the array. 3- A set method to set the array. 4- Sort method to sort the array. 5- Print method to print the array. 6- Reverse method to reverse the...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT