Final- Java Basics ppt Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Sample program

A
public class NameWithSameNameAsFile
{

public static void main (String[] args)
{
System.out.println(“Happy Thanksgiving!”);

} //end main
} //end class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

data types (5)

A

int, double, boolean, char, String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

int

A

whole numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

double

A

numbers with a decimal point

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

boolean

A

true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

char

A

use ‘ ‘ to enclose a single character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String

A

Not a primitive type, notice the capital S. Use “ “

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

For naming, the first character can be

A

a letter, underscore, or $

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Subsequent characters may be

A

letters, numbers, underscores, or $

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

variable names cannot be

A

keywords

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are names case-sensitive?

A

yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

constant names should be

A

ALLCAPS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

variable names

A

shouldBeLikeThis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Variables and constants will be declared

A

at the beginning of the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Variable and constant declaration looks like this:

A
final int MAX_HOURS = 18; 
String name;     
int age;       
double gradePointAvg;   char status;  
boolean active;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

You can initialize a variable as well. This means…

A
to include a value for each variable with the declarations. 
Example:
String name ="Spongebob”;   
int age = 5; 
double gradePointAvg = 3.0;
char status = ‘C’; 
boolean active = true;
17
Q

normal arithmetic operators

A

*

18
Q

division

A

/
If both operands int, returns an int.
If at least one operand double, returns a double.

19
Q

Modulus

A

%

Remainder or leftovers from division.

20
Q

3%10=

A

1

21
Q

5/10=

A

0

22
Q

5.0/10=

A

0.5

23
Q

used for input from the keyboard

A

Scanner class

24
Q

to let Java know that you are going to use the Scanner class

A

add import statement to top of program

25
Q

import statement for Scanner class

A

import java.util.Scanner;

26
Q

After adding the import statement, you must next

A

declare an object in the method that is linked to input from the keyboard

27
Q

Object declared to link input from the keyboard:

A
//declare a Scanner object
Scanner scan = new Scanner (System.in);
28
Q

Scanner methods

A

used in conjunction with scanner objects to get input

29
Q

Scanner methods (list dem)

A

nextLine() for String
nextInt() for integers
nextDouble() for doubles
NOTE: NO boolean or char

30
Q

Example of program that asks for your name and then prints “Hi your name”

A
import java.util.Scanner;
public class UsingScanner
{
public static void main (String[] args)
{
//declare a Scanner object
Scanner scan = new Scanner (System.in);
//declare a variable for name
String name;
//ask user for name
System.out.println("What is your name?");
//use Scanner to get the name and store it in the variable
name = scan.nextLine ();
//print statement
System.out.println ("Hi"+name);
}
}
31
Q

Ask person’s name & age.
Calculate 10 years younger.
Print a nice message.

A
import java.util.Scanner;
public class UsingScanner
{
public static void main (String[] args)
{
//declare a Scanner object
Scanner scan=new Scanner(System.in);
String name;
int age;
int lookLike = 0;

System.out.println ("What's your name?");

name=scan.nextLine();

System.out.println("What's your age?");

age=scan.nextInt();

lookLike = age-10;

System.out.println ("Wow " +name+ " you barely look "+lookLike+ " to me!"); } }