Java Print and Input/Output
16 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What methods have already been used to output strings in Java?

print and println

It is not important to assign some value to a variable before we attempt to use it in any way, including output.

False (B)

What will happen if you give an argument to print or println that is not a string?

It will be converted to a string automatically, and then output.

What operator can be used to combine strings in Java?

<p>concatenation operator (+)</p> Signup and view all the answers

What would the following code print? int height = 4; int width = 6; System.out.println(height + width);

<p>10 (B)</p> Signup and view all the answers

What would the following code print? int height = 4; int width = 6; System.out.println("Dimensions are " + height + " and " + width);

<p>&quot;Dimensions are 4 and 6&quot; (C)</p> Signup and view all the answers

What is the purpose of the In class mentioned in the text?

<p>to simplify input in Java</p> Signup and view all the answers

What happens if incorrect input is entered for a numeric value when using the In class methods?

<p>A value of zero is used. (C)</p> Signup and view all the answers

What is the result if the user hits <enter> during string input when using the In class?

<p>empty string</p> Signup and view all the answers

Given int i = -47, j = 35;, what output will be produced by the following code? System.out.println("The value of i is " + i + " while the value of j is " + j + ".");

<p>The value of i is -47 while the value of j is 35.</p> Signup and view all the answers

Given boolean done = false;, what output will be produced by the following code? System.out.println("We are not " + done + " yet.");

<p>We are not false yet.</p> Signup and view all the answers

Given double x = 0.012, y = 2.7374e1;, what output will be produced by the following code? System.out.println("x -> " + x + "\ny -> " + y);

<p>x -&gt; 0.012\ny -&gt; 27.374</p> Signup and view all the answers

Write a Java program that creates a double variable called myMass, asks the user to give his/her mass, and then prints a message giving the person's mass.

<pre><code class="language-java">import java.util.Scanner; public class MassProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(&quot;Please enter your mass (in kg):&quot;); double myMass = scanner.nextDouble(); System.out.println(&quot;Your mass is: &quot; + myMass + &quot; kg&quot;); scanner.close(); } } </code></pre> Signup and view all the answers

Write a program that creates four integer variables, asks the user to enter four course grades, and then calculates and prints the average grade.

<pre><code class="language-java">import java.util.Scanner; public class AverageGrade { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int grade1, grade2, grade3, grade4; System.out.println(&quot;Enter grade 1:&quot;); grade1 = scanner.nextInt(); System.out.println(&quot;Enter grade 2:&quot;); grade2 = scanner.nextInt(); System.out.println(&quot;Enter grade 3:&quot;); grade3 = scanner.nextInt(); System.out.println(&quot;Enter grade 4:&quot;); grade4 = scanner.nextInt(); double average = (grade1 + grade2 + grade3 + grade4) / 4.0; System.out.println(&quot;The average grade is: &quot; + average); scanner.close(); } } </code></pre> Signup and view all the answers

The area of a circle is $A=πr^2$ and the circumference is $C=2πr$. Write a program that asks the user for a radius and calculates the area and circumference, with each stored in a variable. Your program should include a constant for pi, where $π=3.1415$

<pre><code class="language-java">import java.util.Scanner; public class CircleCalculator { public static void main(String[] args) { final double PI = 3.1415; Scanner scanner = new Scanner(System.in); System.out.println(&quot;Enter the radius of the circle:&quot;); double radius = scanner.nextDouble(); double area = PI * radius * radius; double circumference = 2 * PI * radius; System.out.println(&quot;Area: &quot; + area); System.out.println(&quot;Circumference: &quot; + circumference); scanner.close(); } } </code></pre> Signup and view all the answers

Write and run an interactive program that asks the user to provide a string, a character, an integer, and a floating point value. Your program should also have a constant value built into it. After reading the values, the program should print each one (including the constant) on a separate line with a suitable message for each line of output.

<pre><code class="language-java">import java.util.Scanner; public class InteractiveProgram { public static void main(String[] args) { final int CONSTANT_VALUE = 100; Scanner scanner = new Scanner(System.in); System.out.println(&quot;Enter a string:&quot;); String inputString = scanner.nextLine(); System.out.println(&quot;Enter a character:&quot;); char inputChar = scanner.next().charAt(0); System.out.println(&quot;Enter an integer:&quot;); int inputInt = scanner.nextInt(); System.out.println(&quot;Enter a floating point value:&quot;); double inputFloat = scanner.nextDouble(); System.out.println(&quot;String: &quot; + inputString); System.out.println(&quot;Character: &quot; + inputChar); System.out.println(&quot;Integer: &quot; + inputInt); System.out.println(&quot;Floating Point: &quot; + inputFloat); System.out.println(&quot;Constant Value: &quot; + CONSTANT_VALUE); scanner.close(); } } </code></pre> Signup and view all the answers

Flashcards

print and println

Methods used to display output to the console.

Variable Initialization

Assigning an initial value to a variable when it is declared.

Uninitialized Variable Error

An error that occurs when trying to use a variable without assigning it a value first.

Automatic Type Conversion

The process of converting a variable from one data type to a String type.

Signup and view all the flashcards

String Concatenation

Combining strings together using the '+' operator.

Signup and view all the flashcards

'+' Operator

The symbol '+' which can either add numbers or combine strings.

Signup and view all the flashcards

In and Out Classes

Classes provided to simplify input operations in Java.

Signup and view all the flashcards

In Class

A class containing methods to receive different types of input.

Signup and view all the flashcards

get Methods

Methods within the 'In' class (e.g., getInt, getString) used to read user input of specific data types.

Signup and view all the flashcards

Newline character

The result of pressing 'Enter' after an input.

Signup and view all the flashcards

Null String

An empty string, represented as "".

Signup and view all the flashcards

Input

Receiving data or values from an external source during program execution.

Signup and view all the flashcards

Output

Displaying data or values from the program to an output.

Signup and view all the flashcards

Variable

A named storage location that holds a value.

Signup and view all the flashcards

Data Types

Different classifications of data (e.g., integer, string, character).

Signup and view all the flashcards

Code Fragment 1a

int i = -47, j = 35; System.out.println("The value of i is " + i + " while the value of j is " + j + ".");

Signup and view all the flashcards

Code Fragment 1b

boolean done = false; System.out.println("We are not " + done + " yet.");

Signup and view all the flashcards

Code Fragment 1c

double x = 0.012, y = 2.7374e1; System.out.println("x -> " + x + "\ny -> " + y);

Signup and view all the flashcards

Exercise 2 Program

Program asks for mass prints the input

Signup and view all the flashcards

Exercise 3 Program

Program that gets 4 grades and finds average

Signup and view all the flashcards

Exercise 4a

Ask for mark1, then prints mark 1

Signup and view all the flashcards

Exercise 4b

Ask for mark1, then prints mark 1, ask then print mark2

Signup and view all the flashcards

Exercise 4c

Calculate sum add 2 marks into total mark.

Signup and view all the flashcards

Exercise 4e

Average of mark1, mark2, mark3, mark4 calculated

Signup and view all the flashcards

Exercise 5

Program take radius and computes aread and circumstances.

Signup and view all the flashcards

Exercise 6

Take string, char, int double

Signup and view all the flashcards

System.out.println()

Built in method to display information to the screen.

Signup and view all the flashcards

Initialization

Assigning an initial value to a variable.

Signup and view all the flashcards

Operator Precedence

Mathematical operations that occur before string conversion.

Signup and view all the flashcards

Enter Key

Key pressed to signal end

Signup and view all the flashcards

Study Notes

  • The print and println methods can output strings and values of other variable types.

Printing Variable Values

  • Before using a variable (including for output), it is important to assign it a value, which is good initialization practice.
  • Attempting to output an uninitialized variable will produce an error.

Printing Strings

  • All output in Java is performed using strings.
  • If an argument to print or println is not a string, it is automatically converted to one.
  • The concatenation operator (+) combines strings and can combine any data type in an output statement.
  • The (+) operator is also the arithmetic addition operator, mathematical operations may take precedence over automatic string conversion.

User Input and Output in Java

  • Java is designed for compatibility, input and output can be difficult, custom classes In and Out are available to simplify this process.
  • These classes are not part of standard Java.
  • The In class provides methods for input: getInt, getLong, getChar, getString, getFloat, and getDouble.
  • Each In class method assumes that each input is followed by the <enter> key.
  • If incorrect input is entered for a numeric value, zero will be used.
  • If the user hits <enter> during string input, the result will be an empty, or null, string (represented by "").

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Java Input Output Variables PDF

Description

Explore Java's print and println methods for outputting strings and variable values. Learn about converting data types to strings for printing and using the concatenation operator. Understand basic user input and output facilitated by custom classes In and Out.

More Like This

Use Quizgecko on...
Browser
Browser