Create a java program where you try to declare at least three variables with different data types. Assign the variables to optional values and print them using System.out.println or System.out.print in any way. Name the class Variables. Compare the written java program with the following code. public class NickName { // Main metod public static void main(String[] args) { // Declares four variables (three int and one double) int games; int goals; int shots; double shootingPercentage; // Assigns the variables different values games = 708; goals = 249; shots = 1693; shootingPercentage = 14.7; // Three different ways to print on the screen // 1. Divided into different combinations of print and println // where print does not make a line break, which println does. System.out.print("Peter \"Foppa\" Forsberg did play "); System.out.print(games); System.out.println(" matches in NHL."); // New row with println System.out.print("There he shot totally "); System.out.print(shots); System.out.print(" shoots of which"); System.out.print(goals); System.out.print(" went into goal. \ n"); // Wrap with \ n System.out.print("This he gives a shot percentage of "); System.out.print(shootingPercentage); System.out.println("%"); // 2. Uses + to perform "string addition" System.out.println(); // empty line System.out.println("Peter \"Foppa\" Forsberg did play " + games + " matches in NHL."); System.out.println("There he shot totally " + shots + " shoots of which " + goals + " went into goal."); System.out.println("This gives him a shot percentage of "+ shootingPercentage +"% "); // 3. Uses the format method System.out.println(); // empty line System.out.format("Peter \"Foppa\" Forsberg did play %d matches in NHL.%n", games); System.out.format("There he shot totally %d shoots of which %d went into goal.%n", shots, goals); System.out.format("This gives him a shot percentage of %3.1f%%%n", shootingPercentage); // In the last System.out.format,% 3.1f means that a float (double or float) should be printed // with a total of 3 value digits, of which 2 must belong to the decimal part. // Since% is used in format to specify that a value should be printed, we need to type %% for // that the character% itself should be printed. //% n means new line. } }

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
Create a java program where you try to declare at least three variables with different data types. Assign the variables to optional values and print them using System.out.println or System.out.print in any way. Name the class Variables. 
Compare the written java program with the following code. 
 
public class NickName {
    // Main metod
    public static void main(String[] args) {
        // Declares four variables (three int and one double)
        int games;
        int goals;
        int shots;
        double shootingPercentage;
       
       // Assigns the variables different values
        games = 708;
        goals = 249;
        shots = 1693;
        shootingPercentage = 14.7;
       
       // Three different ways to print on the screen

       // 1. Divided into different combinations of print and println
      // where print does not make a line break, which println does.
        System.out.print("Peter \"Foppa\" Forsberg did play ");
        System.out.print(games);
        System.out.println(" matches in NHL."); // New row with println
        System.out.print("There he shot totally ");
        System.out.print(shots);
        System.out.print(" shoots of which");
        System.out.print(goals);
        System.out.print(" went into goal. \ n"); // Wrap with \ n
        System.out.print("This he gives a shot percentage of ");
        System.out.print(shootingPercentage);
        System.out.println("%");
       
        // 2. Uses + to perform "string addition"
        System.out.println(); // empty line
        System.out.println("Peter \"Foppa\" Forsberg did play " + games + " matches in NHL.");
        System.out.println("There he shot totally " + shots + " shoots of which " + goals + " went into goal.");
        System.out.println("This gives him a shot percentage of "+ shootingPercentage +"% ");

       // 3. Uses the format method
        System.out.println(); // empty line
        System.out.format("Peter \"Foppa\" Forsberg did play %d matches in NHL.%n", games);
        System.out.format("There he shot totally %d shoots of which %d went into goal.%n", shots, goals);
        System.out.format("This gives him a shot percentage of %3.1f%%%n", shootingPercentage);
       // In the last System.out.format,% 3.1f means that a float (double or float) should be printed
// with a total of 3 value digits, of which 2 must belong to the decimal part.
// Since% is used in format to specify that a value should be printed, we need to type %% for
// that the character% itself should be printed.
//% n means new line.
    }
}
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
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