In the real environment we rarely work with the same values all the time. Prompt the user to provide you the integers, take input from console.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
TASK 2. String Formatting.
Review printf() method, Implement the following code, make sure it runs without errors.
3 public class ReportSum2 {
public static void main(String args[]) {
//assume we have 2 numbers a and b, a = 2, b = 5
int a = 2;
int b = 5;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
6
7
9
//assume we have 2 numbers a and b, a = 4, b = 4
a = 4;
b = 4;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
10
11
12
13
14
//assume we have 2 numbers a and b, a = 1000, b = 2000
a = 1000;
b = 2000;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
15
16
17
18
19
//assume we have 2 numbers a and b, a = 100, b = 101
a = 100;
b = 101;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
}
20
21
22
23
24
25 }
In the real environment we rarely work with the same values all the time. Prompt the user to provide you the
integers, take input from console.
Transcribed Image Text:TASK 2. String Formatting. Review printf() method, Implement the following code, make sure it runs without errors. 3 public class ReportSum2 { public static void main(String args[]) { //assume we have 2 numbers a and b, a = 2, b = 5 int a = 2; int b = 5; System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b)); 6 7 9 //assume we have 2 numbers a and b, a = 4, b = 4 a = 4; b = 4; System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b)); 10 11 12 13 14 //assume we have 2 numbers a and b, a = 1000, b = 2000 a = 1000; b = 2000; System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b)); 15 16 17 18 19 //assume we have 2 numbers a and b, a = 100, b = 101 a = 100; b = 101; System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b)); } 20 21 22 23 24 25 } In the real environment we rarely work with the same values all the time. Prompt the user to provide you the integers, take input from console.
Expert Solution
Step 1

To take input from users we have to use the Scanner class.

Scanner sc= new Scanner(System.in);

int a=sc.nextInt();

int b= sc.nextInt();

from like this we can take input from users. After that we will print a,b as well as their sum. We will use here printf method which belongs in Java PrintStream class. We use it to write a String formatted to output Stream.

      It uses the specified format string and arguments.

We can use print/println here, then we don't have to use %4d. We can use then 

        System.out.printf("Provided integers: " +a +"and "+b+ " the total is "+(a+b));

 

Note: In the below code I have used Main as the class name as I am doing my code in the online editor, you can name your class according to your code.

 

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education