Question

Lab Goal :   This lab was designed to teach you how to use while and do...

Lab Goal :   This lab was designed to teach you how to use while and do while loops.

Lab Description :   A perfect number is any number that is equal to the sum of its divisors. 6 is perfect because it has divisors 1, 2, and 3 which sum to 6.   Determine if the provided numbers are perfect. Use mod (%) to find divisors.

Files Needed ::

Perfect.java

PerfectRunner.java

Sample Data :

496

45

6

14

8128

1245

33

28

27

33550336

Sample Output :

496 is perfect.

45 is not perfect.

6 is perfect.

14 is not perfect.

8128 is perfect.

1245 is not perfect.

33 is not perfect.

28 is perfect.

27 is not perfect.

33550336 is perfect.

public class Perfect

{

   private int number;

      //add constructors

      //add a set method

      public boolean isPerfect()

      {

      }

      //add a toString - should display the number and either is or is not perfect

     

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//Perfect.java

public class Perfect

{

      private int number;

      // constructor taking value for number

      public Perfect(int number) {

            this.number = number;

      }

      // set method for number

      public void setNumber(int number) {

            this.number = number;

      }

      // returns true if number is perfect, else false

      public boolean isPerfect() {

            // initializing a variable to keep the sum of factors

            int factors_sum = 0;

            // starting with i=1

            int i = 1;

            // looping as long as i is less than or equal to number/2

            while (i <= number / 2) {

                  // if i divides number evenly, adding i to factors sum

                  if (number % i == 0) {

                        factors_sum += i;

                  }

                  // updating i

                  i++;

            }

            //returns true if factors sum equals number after the loop

            return factors_sum == number;

      }

      // add a toString - should display the number and either is or is not

      // perfect

      @Override

      public String toString() {

            if (isPerfect()) {

                  return number + " is perfect.";

            } else {

                  return number + " is not perfect.";

            }

      }

}

//PerfectRunner.java

import java.util.Scanner;

public class PerfectRunner {

      public static void main(String[] args) {

            // scanner to read user input

            Scanner sc = new Scanner(System.in);

            // looping as long as there are more integers in input. If you are

            // manually entering input values, try entering one value at a time and

            // press ctrl+Z or ctrl+D to trigger end of file/input.

            while (sc.hasNextInt()) {

                  // reading next integer

                  int n = sc.nextInt();

                  // creating a Perfect object

                  Perfect perfect = new Perfect(n);

                  // invoking toString() method, displaying the result

                  System.out.println(perfect);

            }

      }

}

/*OUTPUT for the given input in question*/

496 is perfect.

45 is not perfect.

6 is perfect.

14 is not perfect.

8128 is perfect.

1245 is not perfect.

33 is not perfect.

28 is perfect.

27 is not perfect.

33550336 is perfect.

Add a comment
Know the answer?
Add Answer to:
Lab Goal :   This lab was designed to teach you how to use while and do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • Lab Goal : The lab was designed to teach you more about objects and classes. Lab...

    Lab Goal : The lab was designed to teach you more about objects and classes. Lab Description : In this program, you are to create a Roman Numeral class to handle roman numeral operations. How to convert a Roman Numeral to a standard base ten number : : Locate the first individual roman number in the roman number string. Sum up the numeric value of the individual number. Chop of the individual roman numeral from the string and continue the...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • Lab Description : Write a program that will sum all of a numbers digits. You must...

    Lab Description : Write a program that will sum all of a numbers digits. You must use % for this lab to access the right most digit of the number. You will use /to chop off the right most digit. Sample Data 234 10000 Files Needed: DigitAdder.java JavaLoopLabRunner.java 9005 84645 8547 123456789 55556468 8525455 8514548 1212121212 Sample Output 14 27 24 45 34 35 15 12 import static java.lang.System.* public class DigitAdder public static int go( int num ) return...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • I know how to do number one just not number 2. Please answer number 2 only...

    I know how to do number one just not number 2. Please answer number 2 only Write a java program that will print out following pattern. Use nested for loop. 2. Add a method to the above program that generates takes two numbers as parameters and prints out all the numbers from number 1 to number 2 separated by using a while loop. (Do not print the last ,'). For example: Calling method1(9,51) should print: 9, 10, 11, 12, 13,...

  • Python pleases! Write a program that will produce the following table below. You will use a...

    Python pleases! Write a program that will produce the following table below. You will use a nested for loop and a duplicate nested while loop in one program. Note that this is the one through twelve multiplication table. Write the flowchart for the program. X 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2 3 4 5 6 7 8 9 10 11 12 2 2 4 6 8 10 12 14 16 18...

  • This is my data! How to do all the calculations and everything in lab report? 3...

    This is my data! How to do all the calculations and everything in lab report? 3 A. Standardization of NaOH Solution Trial 1 Trial 2 Trial 3 6 Initial burette reading, HCI 7 Final burette reading, HCI 8 Initial burette reading, NaOH 9 Final burette reading NaoH 10 11 B. Determination of the Molar Mass of an Unknown Acid 12 13 Mass of vial plus contents 14 Mas of vial plus contents less Sample 1 mL mL mL mL mL...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT