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. } }
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. } }
Chapter2: Using Data
Section: Chapter Questions
Problem 14RQ
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.
// 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.
// 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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage