Java Programming Introduction
13 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 class is used in Java to accept input from the user?

  • Console
  • Scanner (correct)
  • Reader
  • Input

Which method should be used if you want to read a floating-point number from the user?

  • nextDouble()
  • nextInt()
  • next()
  • nextFloat() (correct)

What is the purpose of the statement 'Scanner console = new Scanner(System.in);'?

  • To terminate the scanner object
  • To import the Scanner class
  • To create a new scanner that accepts input from the user (correct)
  • To create a scanner that reads from a file

In the provided example, what does the variable 'years' represent?

<p>The years remaining until the user turns 65 (B)</p> Signup and view all the answers

What will be the output if the user inputs '30' for age in the given program?

<p>35 years until retirement! (B)</p> Signup and view all the answers

What does the Math.pow(base, exp) method return?

<p>The base raised to the power of exp (D)</p> Signup and view all the answers

What will be the output of the expression System.out.println(Math.min(3, 7) + 2)?

<p>5 (D)</p> Signup and view all the answers

Which of the following methods returns the absolute value of a number?

<p>Math.abs() (B)</p> Signup and view all the answers

When converting 19/5 to a double using type casting, what is the result?

<p>3.8 (C)</p> Signup and view all the answers

If you call Math.sqrt(121.0), what value will be stored in the variable squareRoot?

<p>11.0 (A)</p> Signup and view all the answers

What would be the output of System.out.println(Math.random())?

<p>A random double between 0 and 1 (C)</p> Signup and view all the answers

Which method would you use to round a number down to the nearest integer?

<p>Math.floor() (C)</p> Signup and view all the answers

What is the value of Math.PI?

<p>3.1415926... (A)</p> Signup and view all the answers

Flashcards

Scanner in Java

A class in Java used to read input from the user; allows receiving different data types (int, double, float, String, char).

Creating Scanner object

Scanner input = new Scanner(System.in); This line creates a Scanner object named 'input', ready to read from the console.

nextInt()

A Scanner method to read an integer from the user's input.

nextDouble()

A Scanner method to read a double-precision floating-point number from the user's input.

Signup and view all the flashcards

Scanner import

import java.util.Scanner; Statement at the beginning of a program to allow using the Scanner class.

Signup and view all the flashcards

Math.abs(value)

Returns the absolute value of the given value. If the value is negative, it returns the positive version of it. If the value is positive, it returns the same value.

Signup and view all the flashcards

Math.ceil(value)

Returns the smallest whole number greater than or equal to the given value.

Signup and view all the flashcards

Math.floor(value)

Returns the largest whole number less than or equal to the given value.

Signup and view all the flashcards

Math.pow(base, exp)

Calculates the base raised to the power of the exponent. It returns the base multiplied by itself exp times.

Signup and view all the flashcards

Math.sqrt(value)

Returns the square root of the given value.

Signup and view all the flashcards

Math.random()

Generates a random double value between 0 (inclusive) and 1 (exclusive).

Signup and view all the flashcards

Math.toDegrees(value)

Converts an angle from radians to degrees. Used for compatibility with trigonometric functions.

Signup and view all the flashcards

Math.toRadians(value)

Converts an angle from degrees to radians. Compatibility with trigonometric functions.

Signup and view all the flashcards

Study Notes

Java Programming Introduction

  • Java is a programming language used to create applications.
  • The Scanner class is used for accepting input from the user.
  • The import java.util.Scanner; line imports the necessary class.
  • A Scanner object is created using Scanner input = new Scanner(System.in);
  • System.in refers to the keyboard input.

Scanner Methods

  • nextInt(): Reads an integer from the user.
  • nextDouble(): Reads a double-precision floating-point number from the user.
  • nextFloat(): Reads a single-precision floating-point number from the user.
  • next(): Reads a string from the user, up to the next whitespace.
  • next().charAt(0): Reads a character from the user input.
  • Each method waits for the user to press Enter.

Example Usage (Scanner)

  • Get user input for age:
System.out.print("How old are you? ");
int age = input.nextInt();
System.out.println("You typed " + age);

Java Math Class

  • Provides mathematical functions.
  • Math.abs(value): Returns the absolute value of a number.
  • Math.ceil(value): Rounds a number up to the nearest integer.
  • Math.floor(value): Rounds a number down to the nearest integer.
  • Math.log10(value): Returns the base-10 logarithm of a number.
  • Math.max(value1, value2): Returns the larger of two values.
  • Math.min(value1, value2): Returns the smaller of two values.
  • Math.pow(base, exp): Returns base raised to the power of exp.
  • Math.random(): Returns a random double between 0.0 (inclusive) and 1.0 (exclusive).
  • Math.round(value): Rounds a number to the nearest integer.
  • Math.sqrt(value): Returns the square root of a number.
  • Math.sin(value), Math.cos(value), Math.tan(value): Trigonometric functions (value in radians).
  • Math.toDegrees(value), Math.toRadians(value): Converts between degrees and radians.
    • Math.E: Euler's number (approximately 2.71828).
    • Math.PI: Pi (approximately 3.14159).
  • Math methods do not print directly to the console.
  • The result is a numerical value

Type Casting

  • Converts a variable from one data type to another.
  • (type) expression: Explicit type casting.
  • Example: (double) 19 / 5 converts the result of the division to a double-precision number.
  • (int) Math.pow(10, 3) truncates Math.pow's results to an integer.

Calling Math Methods

  • Parameters provide input; return values give outputs from the method.

Example: Hypotenuse Calculation

  • Calculate the hypotenuse of a right-angled triangle using user input for the sides
import java.util.Scanner; // Import necessary class
public class Main {
    public static void main(String[] args) {
	Scanner input = new Scanner(System.in); //Create an object from Scanner class
		//Get the inputs from user
		System.out.print("Please Enter the value of side 1");
		double side1 = input.nextDouble();
		System.out.print("Please Enter the value of side 2");
		double side2 = input.nextDouble();
		//Calculate hypotenuse
		double hypotenuse = Math.sqrt((side1 * side1) + (side2 * side2));
		//Display output to user
	System.out.println("The length of the hypotenuse is :"+hypotenuse);
	}
}

Studying That Suits You

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

Quiz Team

Related Documents

Java Programming 1 Lecture PDF

Description

This quiz covers the basics of Java programming, focusing on the Scanner class for user input and various methods to read different data types. It includes examples and explanations to help you understand how to effectively use the Java Math class and input methods. Perfect for beginners looking to solidify their foundational knowledge in Java.

More Like This

Java Scanner Class Quiz
6 questions

Java Scanner Class Quiz

IntuitiveSparrow avatar
IntuitiveSparrow
Using Scanner Methods to Read Data
18 questions
Java Input and Scanner Class
5 questions

Java Input and Scanner Class

LikeMahoganyObsidian avatar
LikeMahoganyObsidian
Java Scanner Class Flashcards
9 questions
Use Quizgecko on...
Browser
Browser