228k views
5 votes
you will write a complete class called SquaresCubes in a file called SquaresCubes.java and upload the file. This class should have the following methods :padString that accepts two parameters: a String and an integer representing a length. The method should pad the parameter string with spaces until its length is the given length. (This is exactly the method you wrote for Exercise 3.17 from Practice-It! website already. Copy that code here)printSquaresCubes that accepts two integers as parameters: a starting number and an ending number. This method should calculate and print the squares and cubes of all the numbers between the starting and ending numbers (both included) in a tabular form as shown below. Use column width=10 for each column plus a single separator space between the columns when you format the table. Use the padString method to get the numbers and titles aligned in the table as shown below.main method that calls the printSquaresCubes method twice: once with 5 and 10 as the starting and ending numbers respectively, and next with 100 and 110 as the starting and ending numbers respectively. main method shouldn't have any print statements. It only has two calls to the printSquaresCubes method.Starting Number: 5 Ending Number: 10 ========== ========== Number Squared ========== ========== 25 36 ========== Cubed ========== 125 216 343 512 729 1000 49 64 ========== 81 100 ========== ========== Starting Number: 100 Ending Number: 110 ========== ========== Number Squared ========== ========== 100 10000 101 10201 102 10404 103 10609 10816 105 11025 106 11236 107 11449 108 11664 109 11881 110 12100 ========== Cubed ========== 1000000 1030301 1061208 1092727 1124864 1157625 1191016 1225043 1259712 1295029 1331000 104 ========== ========== Show Header BJP5 Exercise 3.17: padString Language/Type: Author: $ Java method basics parameters return Strings Roy McElmurry (on 2019/09/19) Write a method padString that accepts two parameters: a String and an integer representing a length. The method should pad the parameter string with spaces until its length is the given length. For example, padString("hello", 8) should return" hello". (This sort of method is useful when trying to print output that lines up horizontally.) If the string's length is already at least as long as the length parameter, your method should return the original string. For example, padString("congratulations", 10) would return "congratulations". Type your solution here: 1 public static String padString (String word, int totalLength) { 2 int len=word. length(); 3 if (totalLength <= len) 4 return (word); 6 for (int i=0; i<(totalLength-len); i++) { 7 word = " " + word; 8 } 9 return(word); 10 }

User Ishika
by
4.1k points

1 Answer

6 votes

Answer:

The Java code is given below

Step-by-step explanation:

// SquaresCubes.java

import java.util.Scanner;

public class SquaresCubes {

public static void main(String args[]) {

int srart, end;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

// Getting the input entered by the user

System.out.print("Starting number :");

srart = sc.nextInt();

System.out.print("Ending number :");

end = sc.nextInt();

System.out.println("============\t============\t============");

System.out.println(padString("Number",12)+""+padString("Squared",16)+""+padString("Cubed",16));

System.out.println("============\t============\t============");

printSquaresCubes(srart, end);

System.out.println("============\t============\t============");

}

private static void printSquaresCubes(int srart, int end) {

for (int i = srart; i <= end; i++) {

System.out.printf("%12d\t%12d\t%12d\\", i, i * i, i * i * i);

}

}

private static String padString(String s, int num) {

int len = s.length();

if (num <= len) {

return s;

}

for (int i = 0; i < (num - len); i++) {

s = " " + s;

}

return s;

}

}

============================

Output:

Starting number :5

Ending number :10

============ ============ ============

Number Squared Cubed

============ ============ ============

5 25 125

6 36 216

7 49 343

8 64 512

9 81 729

10 100 1000

============ ============ ============

========================

Output#2:

Starting number :100

Ending number :110

============ ============ ============

Number Squared Cubed

============ ============ ============

100 10000 1000000

101 10201 1030301

102 10404 1061208

103 10609 1092727

104 10816 1124864

105 11025 1157625

106 11236 1191016

107 11449 1225043

108 11664 1259712

109 11881 1295029

110 12100 1331000

============ ============ ============

User Fractaly
by
4.2k points