slug
type
status
category
summary
date
tags
password
icon
Part 1
Learning Java can be effectively structured around the units you've listed. Here's an essential set of code examples for each unit to help you get started or enhance your learning experience:
txt for typing practice
1: Primitive Types
public class Main {
public static void main(String[] args) {
int number = 5; // integer
double decimal = 5.99; // floating-point number
char letter = 'A'; // character
boolean flag = true; // boolean
System.out.println("Integer: " + number);
System.out.println("Double: " + decimal);
System.out.println("Char: " + letter);
System.out.println("Boolean: " + flag);
}
}
2: Using Objects
public class Main {
public static void main(String[] args) {
String greeting = "Hello, world!";
System.out.println(greeting.toUpperCase()); // using the toUpperCase method from the String class
}
}
3: Boolean Expressions and if Statements
public class Main {
public static void main(String[] args) {
int score = 75;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 80) {
System.out.println("Grade B");
} else if (score >= 70) {
System.out.println("Grade C");
} else {
System.out.println("Grade F");
}
}
}
4: Iteration
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("i = " + i);
}
int j = 0;
while (j < 5) {
System.out.println("j = " + j);
j++;
}
}
}
5: Writing Classes
public class Car {
private String model;
private int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public void display() {
System.out.println("Model: " + model + ", Year: " + year);
}
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2021);
myCar.display();
}
}
6: Array
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
}
}
7: ArrayList
import .util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("John");
names.add("Anna");
names.add("Steve");
for (String name : names) {
System.out.println(name);
}
}
}
8: 2D Array
public class Main {
public static void main(String[] args) {
int[][] matrix = {{1, 2}, {3, 4}};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println("Element at " + i + "," + j + ": " + matrix[i][j]);
}
}
}
}
9: Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat();
myDog.bark();
}
}
10: Recursion
public class Main {
public static int factorial(int n) {
if (n == 1) return 1; // base case
return n * factorial(n - 1); // recursive case
}public static void main(String[] args) {
System.out.println("Factorial of 5 is " + factorial(5));
}
}
UNIT 1: Primitive Types
UNIT 2: Using Objects
UNIT 3: Boolean Expressions and if Statements
UNIT 4: Iteration
UNIT 5: Writing Classes
UNIT 6: Array
UNIT 7: ArrayList
UNIT 8: 2D Array
UNIT 9: Inheritance
UNIT 10: Recursion
Part 2
For a student learning AP Computer Science A, it's important to grasp fundamental Java programming concepts and structures. Here's a list of essential Java code snippets that cover the core topics generally included in the AP Computer Science A curriculum:
1. Hello World - Basic Program Structure
2. Variables and Data Types
3. Conditionals - If-Else Statements
4. Loops - For Loop
5. While Loop
6. Arrays
7. Methods
8. Object-Oriented Programming - Classes and Objects
9. Using ArrayList
10. Inheritance
11. Polymorphism
12. Recursion
Part 3 Quiz
Certainly! Here are the 20 multiple-choice questions (MCQs) based on the content of your PDF file without the answers provided:
- Primitive Types: What does the following line of Java code initialize?
A) A character
B) An integer
C) A floating-point number
D) A boolean
- Using Objects: What does the
toUpperCase()
method do to the stringgreeting
in the code below?
A) Converts it to lowercase
B) Converts it to uppercase
C) Reverses the string
D) Trims whitespace from the string
- Boolean Expressions and if Statements: What is the output of the following code if
score
is 85?
A) Grade A
B) Grade B
C) Grade C
D) Grade F
- Iteration: Which statement best describes the purpose of the
for
loop in Java? A) Executes a block of statements repeatedly as long as a specified condition is true B) Executes a block of statements for each element in a collection C) Only executes a block of statements if a condition is true D) None of the above
- Writing Classes: What is encapsulated within the
Car
class as defined above? A) Only methods B) Only fields C) Both fields and methods D) Neither fields nor methods
- Array: What is the type of elements stored in the array
numbers
?
A)
int
B) String
C) boolean
D) double
- ArrayList: What does the
add
method do in anArrayList
? A) Deletes an element from the list B) Adds an element to the list C) Finds an element in the list D) Updates an element in the list
- 2D Array: What does the following line of code output?
A) 1
B) 2
C) 3
D) 4
- Inheritance: Which class does
Dog
inherit from? A) Animal B) Main C) System D) Object
- Recursion: What is the base case in the recursive
factorial
method? A)n == 1
B)n == 0
C)n < 1
D)n > 1
- Primitive Types: What type of data does the following Java line declare?
A) Integer
B) Character
C) Floating-point number
D) Boolean
- Using Objects: Which class does the method
toUpperCase()
belong to? A) Object B) System C) Main D) String
- Boolean Expressions and if Statements: What will the following code snippet print?
A) Greater than 20
B) 20 or less
C) No output
D) Error
- Iteration: What is the output of the following
while
loop?
A) i = 0, i = 1, i = 2
B) i = 1, i = 2, i = 3
C) i = 0, i = 1, i = 2, i = 3
D) No output
- Writing Classes: What will the following code snippet output?
A) Model: Honda, Year: 2020
B) Honda, 2020
C) Display Honda 2020
D) Error
- Array: What is the length of the array defined as follows?
A) 4
B) 5
C) 6
D) 10
- ArrayList: What will the following code output?
A) Alice
B) Bob
C) Charlie
D) None of the above
- 2D Array: How many rows does the following 2D array have?
A) 2
B) 3
C) 4
D) 6
- Inheritance: What feature of OOP does the following Java code demonstrate?
A) Encapsulation
B) Inheritance
C) Polymorphism
D) Composition
- Recursion: How many recursive calls are made when calculating
factorial(5)
using the following method?
A) Four
B) Five
C) Six
D) Seven
Answers
- D
- B
- B
- A
- C
- A
- B
- B
- A
- A
- C
- D
- B
- A
- A
- B
- B
- B
- B
- A
Encapsulation问题详解
在面向对象编程中,封装(Encapsulation)是指将数据(字段)和操作数据的方法捆绑到一个单独的单元或类中。封装有助于保护数据,防止外部干扰和误用,这是面向对象编程的基本原则之一。
让我们来看一下
Car
类的定义:字段(Fields):
private String color;
private int year;
这些是类的数据成员或字段。它们存储
Car
对象的状态(即车的颜色和年份)。方法(Methods):
public void displayInfo()
这个方法定义了
Car
类的行为,它操作字段(如color
和year
),并打印出车的信息。为什么是“字段和方法”呢?
- 字段用来存储数据,方法用来定义可以对这些数据进行的操作。
- 封装将数据和操作这些数据的方法放在同一个类中,确保对象的内部状态只能通过方法来访问和修改(方法可以控制访问规则)。
在
Car
类中,既有字段(color
、year
),也有方法(displayInfo()
),因此C) Both fields and methods(字段和方法)是正确答案。- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/csawarmup
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。