练习 !
Code Gym 任务 —— 整数类型转换联系
—— Java 基础,Level 10, Lesson 8。
任务 1 —— 关于整数类型转换
正确安排强制转换运算符以获得所需的结果:d > 0
int a = 0;
int b = (byte) a + 46;
byte c = (byte) (a*b);
double f = (char) 1234.15;
long d = (short) (a + f / c + b);
答:
package zh.codegym.task.task10.task1001;
/*
任务 1 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
int a = 0;
int b = a + 46;
byte c = (byte) (a * b);
double f = 1234.15;
long d = (long) (a + f / c + b);
System.out.println(d);
}
}
任务 2 —— 关于整数类型转换
正确安排强制转换运算符以获得所需的结果:d = 3.765
int a = 15;
int b = 4;
float c = a / b;
double d = a * 1e-3 + c;
答:
package zh.codegym.task.task10.task1002;
/*
任务 2 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
int a = 15;
int b = 4;
float c = (float)a / b;
double d = a * 1e-3 + c;
System.out.println(d);
}
}
任务 3 —— 关于整数类型转换
添加一个类型转换以获得如下答案:b = 0
float f = (float)128.50;
int i = (int)f;
int b = (int)(i + f);
答:
package zh.codegym.task.task10.task1003;
/*
任务 3 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
float f = (float) 128.50;
int i = (int) f;
int b = (int) ((byte)i + f);
System.out.println(b);
}
}
任务 4 —— 关于整数类型转换
添加一个类型转换以获得如下答案:nine = 9
short number = 9;
char zero = ‘0’;
int nine = (zero + number);
答:
package zh.codegym.task.task10.task1004;
/*
任务 4 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
short number = 9;
char zero = '0';
int nine = ((int)zero + number);
System.out.println((char)nine);
}
}
任务 5 —— 关于整数类型转换
正确安排强制转换运算符以获得如下答案:c = 256
int a = (byte)44;
int b = (byte)300;
short c = (byte)(b - a);
答:
package zh.codegym.task.task10.task1005;
/*
任务 5 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
int a = (short) 44;
int b = (short) 300;
short c = (short) (b - a);
System.out.println(c);
}
}
任务 6 —— 关于整数类型转换
删除不必要的运算符以获得正确的答案:结果:1000.0
double d = (short) 2.50256e2d;
char c = (short) ‘d’;
short s = (short) 2.22;
int i = (short) 150000;
float f = (short) 0.50f;
double result = f + (i / c) - (d * s) - 500e-3;
答:
package zh.codegym.task.task10.task1006;
/*
任务 6 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
double d = (short) 2.50256e2d;
char c = (short) 'd';
short s = (short) 2.22;
int i = 150000;
float f = 0.50f;
double result = f + (i / c) - (d * s) - 500e-3;
System.out.println("结果: " + result);
}
}
任务 7 —— 关于整数类型转换
删除不必要的运算符,以获得正确的答案:1234567
long l = (byte)1234_564_890L;
int x = (byte)0b1000_1100_1010;
double m = (byte)110_987_654_6299.123_34;
float f = (byte)l++ + 10 + ++x - (float)m;
l = (long) f / 1000;
答:
package zh.codegym.task.task10.task1007;
/*
任务 7 - 关于整数类型转换
*/
public class Solution {
public static void main(String[] args) {
long l = 1234_564_890L;
int x = 0b1000_1100_1010;
double m = (byte) 110_987_654_6299.123_34;
float f = l++ + 10 + ++x - (float) m;
l = (long) f / 1000;
System.out.println(l);
}
}
Code Gym 任务 —— 目标 1 的最终任务
—— Java 基础,Level 10, Lesson 11。
任务 1 —— 正确答案:d = 2.941
添加一个类型转换以获得如下答案:d = 2.941
示例输出:
2.9411764705882355
答:
package zh.codegym.task.task10.task1008;
/*
正确答案:d = 2.941
*/
public class Solution {
public static void main(String[] args) {
int a = 50;
int b = 17;
double d = (double)a / b;
System.out.println(d);
}
}
任务 2 —— 正确答案:d = 5.5
添加一个类型转换以获得如下答案:d = 5.5
答:
package zh.codegym.task.task10.task1009;
/*
正确答案:d = 5.5
*/
public class Solution {
public static void main(String[] args) {
int a = 5;
int b = 4;
int c = 3;
int e = 2;
double d = a + b / c / (double) e;
System.out.println(d);
}
}
任务 3 —— 正确答案:d = 1.0
添加一个类型转换以获得如下答案:d = 1.0
答:
package zh.codegym.task.task10.task1010;
/*
正确答案:d = 1.0
*/
public class Solution {
public static void main(String[] args) {
int a = 257;
int b = 4;
int c = 3;
int e = 2;
double d = (byte)a + b / c / e;
System.out.println(d);
}
}
任务 4 —— 高薪
按如下模式显示***“我不想学习 Java。我要高薪***”15 次。

答:
package zh.codegym.task.task10.task1011;
/*
高薪
*/
public class Solution {
public static void main(String[] args) {
String s = "我不想学习 Java。我要高薪";
for (int i = 0; i < 15; i++) {
System.out.println(s);
s = s.substring(1);
}
//在此编写你的代码
}
}
任务 5 —— 字母数
从键盘输入 10 个字符串,并计算其中的不同字母数(针对 26 个小写英文字母)。在屏幕上按字母顺序显示结果。

答:
package zh.codegym.task.task10.task1012;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
字母数
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// 字母
String abc = "abcdefghijklmnopqrstuvwxyz";
char[] abcArray = abc.toCharArray();
ArrayList<Character> alphabet = new ArrayList<>();
for (char letter : abcArray) {
alphabet.add(letter);
}
// 读取字符串
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String s = reader.readLine();
list.add(s.toLowerCase());
}
// 在此编写你的代码
int[] letterCount= new int[26];
for (int i = 0; i < 25; i++) {
letterCount[i] =0;
}
int[] count = new int[alphabet.size()];
for (String s : list) {
for (char c : s.toCharArray()) {
int index = alphabet.indexOf(c);
if (index < 0) {
continue;
}
count[index]++;
}
}
for (int i = 0; i < alphabet.size(); i++) {
char c = alphabet.get(i);
System.out.println(c + " " + count[i]);
}
}
}
任务 6 —— Human 类构造函数
写出包含 6 个字段的 Human 类。
想一想,实施 10 个不同的构造函数。
每个构造函数都应该有意义。
答:
package zh.codegym.task.task10.task1013;
/*
Human 类构造函数
*/
public class Solution {
public static void main(String[] args) {
}
public static class Human {
private String name;
private boolean gender;
private int age;
private int score;
private String phone;
private String id;
// 在此写变量和构造函数
public Human(String name, boolean gender, int age, int score, String phone, String id) {
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
this.phone = phone;
this.id = id;
}
public Human(String name) {
this.name = name;
}
public Human(int age) {
this.age = age;
}
public Human(String name, boolean gender) {
this.name = name;
this.gender = gender;
}
public Human(String name, boolean gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public Human(int age, int score) {
this.age = age;
this.score = score;
}
public Human(int score, String phone) {
this.score = score;
this.phone = phone;
}
public Human(int age, int score, String phone, String id) {
this.age = age;
this.score = score;
this.phone = phone;
this.id = id;
}
public Human(String name, boolean gender, int age, int score, String phone) {
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
this.phone = phone;
}
public Human(String name, boolean gender, int age, int score) {
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
}
}
}
任务 7 —— 尽量不用 static 修饰符
使用尽可能少的 static 修饰符以使示例进行编译。
答:
package zh.codegym.task.task10.task1014;
/*
尽量不用 static 修饰符
*/
public class Solution {
public int A = 5;
public int B = 5;
public int C = 5;
public static int D = 5;
public void main(String[] args) {
Solution solution = new Solution();
solution.A = 5;
solution.B = 5 * B;
solution.C = 5 * C * D;
Solution.D = 5 * D * C;
Solution.D = 5;
}
public int getA() {
return A;
}
}
任务 8 —— 字符串列表数组
创建一个元素是字符串列表的数组。
在数组中填入任意数据并在屏幕上显示。
答:
package zh.codegym.task.task10.task1015;
import java.util.ArrayList;
/*
字符串列表数组
*/
public class Solution {
public static void main(String[] args) {
ArrayList<String>[] arrayOfStringList = createList();
printList(arrayOfStringList);
}
public static ArrayList<String>[] createList() {
//在此编写你的代码
ArrayList<String>[] arr = (ArrayList<String>[])new ArrayList[2];
ArrayList<String> list0 = new ArrayList<String>();
ArrayList<String> list1 = new ArrayList<String>();
list0.add("s");
list0.add("sad");
list1.add("asd12");
list1.add("asdf2v");
arr[1] = list1;
arr[0] = list0;
return arr;
}
public static void printList(ArrayList<String>[] arrayOfStringList) {
for (ArrayList<String> list : arrayOfStringList) {
for (String s : list) {
System.out.println(s);
}
}
}
}
任务 9 —— 列表中的相同词
从键盘读取 20 个单词的列表。
你要计算每个单词出现在列表中的次数。
结果应表示为 Map<String, Integer>,其中键是唯一单词,而且值是该单词出现在列表中的次数。
显示映射的内容。
大小写(大写/小写)会影响结果。
答:
package zh.codegym.task.task10.task1016;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/*
列表中的相同词
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> words = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
words.add(reader.readLine());
}
Map<String, Integer> map = countWords(words);
for (Map.Entry<String, Integer> pair : map.entrySet()) {
System.out.println(pair.getKey() + " " + pair.getValue());
}
}
public static Map<String, Integer> countWords(ArrayList<String> list) {
HashMap<String, Integer> result = new HashMap<String, Integer>();
//在此编写你的代码
for (String s : list) {
if (!result.containsKey(s)){
result.put(s, 1);
}else {
result.put(s, result.get(s)+1);
}
}
return result;
}
}
任务 10 —— 功能还不够!
任务:程序从键盘读取一对(一个数字和字符串)并在屏幕上显示。
新任务:程序从键盘读取对(一个数字和字符串),并将其存储在 HashMap。
任何空的输入字符都表示数据输入结束。
这些数字可以重复。
字符串始终是唯一的。
输入的数据不得丢失!
然后该程序在屏幕上显示 HashMap 的内容。
每个新行显示一对。

答:
package zh.codegym.task.task10.task1019;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/*
功能还不够!
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
HashMap<String, Integer> map = new HashMap<String, Integer>();
while (true) {
String number = reader.readLine();
if (number.isEmpty()) {
break;
}
int id = Integer.parseInt(number);
String name = reader.readLine();
map.put(name, id);
}
for (Map.Entry<String, Integer> s : map.entrySet()) {
System.out.println(s.getValue() + " " + s.getKey());
}
}
}
任务 11 —— 需要更正
任务:该程序演示了 HashMap 的工作原理:从键盘读取一组对(数字和字符串),将其放在一个 HashMap 中,然后显示 HashMap 的内容。
答:
package zh.codegym.task.task10.task1018;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/*
需要更正
*/
public class Solution {
HashMap<Integer, String> map;
static Integer index;
static String name;
public Solution() {
this.map = new HashMap<Integer, String>();
}
public static void main(String[] args) throws IOException {
Solution solution = new Solution();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 10; i++) {
int index = Integer.parseInt(reader.readLine());
String name = reader.readLine();
solution.map.put(index, name);
}
for (Map.Entry<Integer, String> pair : solution.map.entrySet()) {
index = pair.getKey();
name = pair.getValue();
System.out.println("Id=" + index + " Name=" + name);
}
}
}
Code Gym 任务 —— 继承练习题
—— Java 核心,Level 1,Lesson 6。
任务 1 —— Horse 和 Pegasus
编辑以下两个类:Horse 和 Pegasus。
让 Pegasus 类继承 Horse 类。
答:
package zh.codegym.task.task11.task1101;
/*
Horse 和 Pegasus
*/
public class Solution {
public static void main(String[] args) {
}
public class Horse {
}
public class Pegasus extends Horse{
}
}
任务 2 —— 宠物
编辑以下三个类:Pet、Cat 和 Dog。
让 Cat 和 Dog 类继承 Pet 类。
答:
package zh.codegym.task.task11.task1102;
/*
宠物
*/
public class Solution {
public static void main(String[] args) {
}
public class Pet {
}
public class Cat extends Pet {
}
public class Dog extends Pet{
}
}
任务 3 —— 星际农场
编写以下六个类:Animal、Cow、Pig、Sheep、Bull 和 Goat。
让 Cow、Pig、Sheep、Bull 和 Goat 类继承 Animal 类。
答:
package zh.codegym.task.task11.task1103;
/*
星际农场
*/
public class Solution {
public static void main(String[] args) {
}
public class Animal {
}
public class Cow extends Animal {
}
public class Pig extends Animal{
}
public class Sheep extends Animal{
}
public class Bull extends Animal{
}
public class Goat extends Animal{
}
}
任务 4 —— 勤奋工作者
编写以下四个类:Employee、Manager、CEO 和 Secretary。
让 Employee、Manager、CEO 和 Secretary 类继承 Employee 类。
答:
package zh.codegym.task.task11.task1104;
/*
勤奋工作者
*/
public class Solution {
public static void main(String[] args) {
}
public class Manager extends Employee {
}
public class CEO extends Employee {
}
public class Employee {
}
public class Secretary extends Employee {
}
}
任务 5 —— IT 公司
编写以下九个类:Employee、Clerk、ITSpecialist、Programmer、ProjectManager、CTO、Recruiter、OfficeManager 和 Custodian。
让 Programmer、ProjectManager、CTO 类继承 *ITSpecialist* 类。
让 Recruiter、Custodian 和 OfficeManager 类继承 *Clerk* 类。
让 Clerk 和 ITSpecialist 类继承 *Employee* 类。
答:
package zh.codegym.task.task11.task1105;
/*
IT 公司
*/
public class Solution {
public static void main(String[] args) {
}
public class Employee {
}
public class Clerk extends Employee{
}
public class ITSpecialist extends Employee {
}
public class Programmer extends ITSpecialist {
}
public class ProjectManager extends ITSpecialist {
}
public class CTO extends ITSpecialist{
}
public class OfficeManager extends Clerk {
}
public class Recruiter extends Clerk {
}
public class Custodian extends Clerk {
}
}
Code Gym 任务 —— 封装练习题
—— Java 核心,Level 1,Lesson 8。
任务 1 —— 隐蔽包装的猫
将 Cat 类的所有内部变量设为 private。
答:
package zh.codegym.task.task11.task1106;
/*
隐蔽包装的猫
*/
public class Solution {
public static void main(String[] args) {
}
public class Cat {
private String name;
private int age;
private int weight;
public Cat() {
}
public Cat(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
}
}
任务 2 —— 我们的猫太公开啦!
将 Cat 类的部分内部变量设为 private,不过只能是那些可通过方法访问的变量。
Requirements:
- Cat 类中的 name 变量必须为 private。
- Cat 类中的 age 变量必须为 private。
- Cat 类中的 weight 变量必须为 public。
- Cat 类中必须有三个变量。
- Cat 类必须包含 private 和 public 变量。
答:
package zh.codegym.task.task11.task1107;
/*
我们的猫太公开啦!
*/
public class Solution {
public static void main(String[] args) {
}
public class Cat {
private String name;
private int age;
public int weight;
public Cat(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
任务 3 —— 不可触摸的猫
隐藏 Cat 类所有内部变量以及能够改变 Cat 对象的内部状态的任何方法。
答:
package zh.codegym.task.task11.task1108;
/*
不可触摸的猫
*/
public class Solution {
public static void main(String[] args) {
}
public class Cat {
private String name;
private int age;
private int weight;
public Cat(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
private void setAge(int age) {
this.age = age;
}
}
}
任务 4 —— 猫狗示例
将 Cat 和 Dog 类的所有内部变量设为 private。
另外,将所有方法设为 private,除了那些允许这两个类相互交互的方法。
答:
package zh.codegym.task.task11.task1109;
/*
猫狗示例
*/
public class Solution {
public static void main(String[] args) {
Cat cat = new Cat("奥斯卡", 5);
Dog dog = new Dog("流浪者", 4);
cat.isDogNear(dog);
dog.isCatNear(cat);
}
}
class Cat {
private String name;
private int speed;
public Cat(String name, int speed) {
this.name = name;
this.speed = speed;
}
private String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public boolean isDogNear(Dog dog) {
return this.speed > dog.getSpeed();
}
}
class Dog {
private String name;
private int speed;
public Dog(String name, int speed) {
this.name = name;
this.speed = speed;
}
private String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public boolean isCatNear(Cat cat) {
return this.speed > cat.getSpeed();
}
}
任务 5 —— 别忘了封装
仔细检查方法,然后添加缺少的字段 。
答:
package zh.codegym.task.task11.task1110;
/*
别忘了封装
*/
public class Solution {
public static void main(String[] args) {
}
public class Cat {
private String name;
private int age;
private int weight;
private int speed;
public Cat(String name, int age, int weight) {
}
public String getName() {
return null;
}
public int getAge() {
return 0;
}
private void setWeight(int weight) {
}
private void setSpeed(int speed) {
}
}
}
Code Gym 任务 —— 作业与奖励练习题
—— Level 1, Lesson 11.
任务 1 —— “亚当和夏娃”
编辑两个类:Adam 和 Eve。
让 Eve 继承 Adam。
答:
package zh.codegym.task.task11.task1111;
/*
“亚当和夏娃”
*/
public class Solution {
public static void main(String[] args) {
}
// 亚当
public class Adam {
}
// 夏娃
public class Eve extends Adam {
}
}
任务 2 —— 亦敌亦友
编辑 AppleIPhone 和 SamsungGalaxyS2 类。
让 SamsungGalaxyS2 类继承 AppleIPhone 类。
答:
package zh.codegym.task.task11.task1112;
/*
亦敌亦友
*/
public class Solution {
public static void main(String[] args) {
}
public class AppleIPhone {
}
public class SamsungGalaxyS2 extends AppleIPhone{
}
}
任务 3 —— 玩转进化论
编辑以下四个类:Fish、Animal、Ape 和 Human。
让 Animal 继承 Fish,Ape 继承 Animal,以及 Human 继承 Ape。
答:
package zh.codegym.task.task11.task1113;
/*
玩转进化论
*/
public class Solution {
public static void main(String[] args) {
}
public class Fish {
}
public class Animal extends Fish{
}
public class Ape extends Animal {
}
public class Human extends Ape{
}
}
任务 4 —— 不断演变的生物
编辑以下三个类:Fish、Lizard 和 Dinosaur。
让 Lizard 继承 Fish,Dinosaur 继承 Lizard。
答:
package zh.codegym.task.task11.task1114;
/*
不断演变的生物
*/
public class Solution {
public static void main(String[] args) {
}
public class Fish {
}
public class Lizard extends Fish{
}
public class Dinosaur extends Lizard {
}
}
任务 5 —— 从学生到熟练工
编辑以下四个类:Schoolboy、Student、Employee 和 Slave。
让 Student 继承 Schoolboy,Employee 继承 Student 以及 Slave 继承 Employee。
答:
package zh.codegym.task.task11.task1115;
/*
从学生到熟练工
*/
public class Solution {
public static void main(String[] args) {
}
public class Schoolboy {
}
public class Student extends Schoolboy{
}
public class Employee extends Student{
}
public class Slave extends Employee {
}
}
任务 6 —— 继承链
为以下类创建一个适当的“继承链”:Pet、Cat 和 Dog。
答:
package zh.codegym.task.task11.task1116;
/*
继承链
*/
public class Solution {
public static void main(String[] args) {
}
public class Pet {
}
public class Cat extends Pet{
}
public class Dog extends Pet {
}
}
任务 7 —— 其他继承链
为以下类创建一个适当的“继承链”:Carnivore、Cow、Dog、Pig 和 Animal。
答:
package zh.codegym.task.task11.task1117;
/*
其他继承链
*/
public class Solution {
public static void main(String[] args) {
}
public class Carnivore extends Animal {
}
public class Cow extends Animal{
}
public class Dog extends Carnivore {
}
public class Pig extends Animal{
}
public class Animal {
}
}
任务 8 —— 城市家庭
为以下类创建一个适当的“继承链”:Pet、Cat、Dog 和 Car。
答:
package zh.codegym.task.task11.task1118;
/*
城市家庭
*/
public class Solution {
public static void main(String[] args) {
}
public class Pet {
}
public class Cat extends Pet {
}
public class Car {
}
public class Dog extends Pet {
}
}
任务 9 —— 正确的继承链:第 4 部分
为以下类创建一个适当的“继承链”:House、Cat、Dog 和 Car。
答:
package zh.codegym.task.task11.task1119;
/*
正确的继承链:第 4 部分
*/
public class Solution {
public static void main(String[] args) {
}
public class House {
}
public class Cat {
}
public class Car {
}
public class Dog {
}
}
任务 10 —— 正确的继承链:第 5 部分
为以下类创建一个适当的“继承链”:House、Cat、Dog、Car、Animal 和 Asset。
答:
package zh.codegym.task.task11.task1120;
/*
正确的继承链:第 5 部分
*/
public class Solution {
public static void main(String[] args) {
}
public class House extends Asset {
}
public class Cat extends Animal {
}
public class Car extends Asset {
}
public class Dog extends Animal {
}
public class Animal {
}
public class Asset {
}
}
任务 11 —— 奇怪的陌生代码
更正以下类的继承:(Cat、Dog、Pet、House 和 Airplane 类)
答:
package zh.codegym.task.task11.task1121;
/*
奇怪的陌生代码
*/
public class Solution {
public static void main(String[] args) {
}
public class Pet {
}
public class Cat extends Pet {
}
public class Dog extends Pet {
}
public class House {
}
public class Airplane {
}
}
任务 12 —— 拯救国际象棋俱乐部
在每种象棋类中添加共同基础类 ChessPiece。
答:
package zh.codegym.task.task11.task1122;
/*
拯救国际象棋俱乐部
*/
public class Solution {
public static void main(String[] args) {
}
public class King extends ChessPiece {
}
public class Queen extends ChessPiece {
}
public class Rook extends ChessPiece {
}
public class Knight extends ChessPiece {
}
public class Bishop extends ChessPiece {
}
public class Pawn extends ChessPiece {
}
public class ChessPiece {
}
}
任务 13 —— 最小和最大
编写返回数组的最小和最大****数字的方法。
答:
package zh.codegym.task.task11.task1123;
public class Solution {
public static void main(String[] args) throws Exception {
int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5};
Pair<Integer, Integer> result = getMinimumAndMaximum(data);
System.out.println("最小值为 " + result.x);
System.out.println("最大值为 " + result.y);
}
public static Pair<Integer, Integer> getMinimumAndMaximum(int[] array) {
if (array == null || array.length == 0) {
return new Pair<Integer, Integer>(null, null);
}
int minimum = array[0];
int maximum = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < minimum)
minimum = array[i];
if (array[i] > maximum)
maximum = array[i];
}
return new Pair<Integer, Integer>(minimum, maximum);
}
public static class Pair<X, Y> {
public X x;
public Y y;
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
}
}
Code Gym 任务 —— 多态任务
—— Level 2,Lesson 2。
任务 1 —— 鲸鱼和奶牛
重写 Whale 类中的 getName 方法,从而使程序显示:
我不是奶牛。我是一条鲸鱼。
答:
package zh.codegym.task.task12.task1201;
/*
鲸鱼和奶牛
*/
public class Solution {
public static void main(String[] args) {
Cow cow = new Whale();
System.out.println(cow.getName());
}
public static class Cow {
public String getName() {
return "我是一头牛";
}
}
public static class Whale extends Cow {
@Override
public String getName(){
return "我不是奶牛。我是一条鲸鱼。";
}
}
}
任务 2 —— 鲸鱼,母牛的后代
重写 Whale 类中的 getName 方法,从而使程序不在屏幕上显示任何内容。
答:
package zh.codegym.task.task12.task1202;
/*
鲸鱼,母牛的后代
*/
public class Solution {
public static void main(String[] args) {
Cow cow = new Whale();
System.out.println(cow.getName());
}
public static class Cow {
public String getName() {
return "我是一头牛";
}
}
public static class Whale extends Cow {
@Override
public String getName() {
return "";
}
}
}
任务 3 —— 物归原主
重写 Cat 和 Dog 类中的 getChild 方法,以便猫生猫,狗生狗。
答:
package zh.codegym.task.task12.task1203;
/*
物归原主
*/
public class Solution {
public static void main(String[] args) {
Pet pet1 = new Cat();
Pet cat = pet1.getChild();
Pet pet2 = new Dog();
Pet dog = pet2.getChild();
}
public static class Pet {
public Pet getChild() {
return new Pet();
}
}
public static class Cat extends Pet {
@Override
public Cat getChild() {
return new Cat();
}
}
public static class Dog extends Pet {
@Override
public Dog getChild() {
return new Dog();
}
}
}
任务 4 —— 无论是鸟还是灯
编写一种方法,该方法确定传递给它的对象的类类型,然后在屏幕上显示相应的消息:Cat、Dog、Bird 和 Lamp。
答:
package zh.codegym.task.task12.task1204;
/*
无论是鸟还是灯
*/
public class Solution {
public static void main(String[] args) {
printObjectType(new Cat());
printObjectType(new Bird());
printObjectType(new Lamp());
printObjectType(new Cat());
printObjectType(new Dog());
}
public static void printObjectType(Object o) {
if (o instanceof Cat)
System.out.println("Cat");
if (o instanceof Dog)
System.out.println("Dog");
if (o instanceof Bird)
System.out.println("Bird");
if (o instanceof Lamp)
System.out.println("Lamp");
}
public static class Cat {
}
public static class Dog {
}
public static class Bird {
}
public static class Lamp {
}
}
任务 5 —— 动物识别
编写一个方法,该方法将确定传递给它的对象的类,并返回以下值之一:“奶牛”、“鲸鱼”、“狗”或“未知动物”。
答:
package zh.codegym.task.task12.task1205;
/*
动物识别
*/
public class Solution {
public static void main(String[] args) {
System.out.println(getObjectType(new Cow()));
System.out.println(getObjectType(new Dog()));
System.out.println(getObjectType(new Whale()));
System.out.println(getObjectType(new Pig()));
}
public static String getObjectType(Object o) {
//在此编写你的代码
if (o instanceof Dog){
return "狗";
}
if (o instanceof Cow){
return "奶牛";
}
if (o instanceof Whale){
return "鲸鱼";
}
else {
return "未知动物";
}
}
public static class Cow {
}
public static class Dog {
}
public static class Whale {
}
public static class Pig {
}
}
Code Gym 任务 —— 练习重载方法
Level 2,Lesson 4。
任务 1 —— 我们超负荷了!
编写以下两种方法:print(int) 和 print(String)。
答:
package zh.codegym.task.task12.task1206;
/*
我们超负荷了!
*/
public class Solution {
public static void main(String[] args) {
}
public static void print(int a){
System.out.println(a);
}
public static void print(String a){
System.out.println(a);
}
}
任务 2 —— int 和 Integer
编写以下两种方法:print(int) 和 print(Integer)。
在 main 方法中,编写调用两种方法的代码:
答:
package zh.codegym.task.task12.task1207;
/*
int 和 Integer
*/
public class Solution {
public static void main(String[] args) {
print(50);
print((Integer) 50);
}
//在此编写你的代码
public static void print(int a){
System.out.println(a);
}
public static void print(Integer a){
System.out.println(a);
}
}
任务 3 —— 出版自由
使用不同的参数编写 5 种 print 方法。
答:
package zh.codegym.task.task12.task1208;
/*
出版自由
*/
public class Solution {
public static void main(String[] args) {
}
//在此编写你的代码
public static void print(String s) {
System.out.println(s);
}
public static void print(int s) {
System.out.println(s);
}
public static void print(char s) {
System.out.println(s);
}
public static void print(double s) {
System.out.println(s);
}
public static void print(long s) {
System.out.println(s);
}
}
任务 4 —— 三种方法和最小值
编写以下 public static 方法:int min(int, int)、long min(long, long)、double min(double, double)。
每个方法必须返回传递给它的两个数字中的最小值。
答:
package zh.codegym.task.task12.task1209;
/*
三种方法和最小值
*/
public class Solution {
public static void main(String[] args) {
}
//在此编写你的代码
public static int min(int a, int b) {
if (a < b) {
return a;
} else {
return b;
}
}
public static long min(long a, long b) {
if (a < b) {
return a;
} else {
return b;
}
}
public static double min(double a, double b) {
if (a < b) {
return a;
} else {
return b;
}
}
}
任务 5 —— 三种方法和最大值
编写以下 public static 方法:int max(int, int)、long max(long, long)、double max(double, double)。
每个方法必须返回传递给它的两个数字中的最大值。
答:
package zh.codegym.task.task12.task1210;
/*
三种方法和最大值
*/
public class Solution {
public static void main(String[] args) {
}
//在此编写你的代码
public static int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
public static long max(long a, long b) {
if (a > b) {
return a;
} else {
return b;
}
}
public static double max(double a, double b) {
if (a > b) {
return a;
} else {
return b;
}
}
}
Code Gym 任务 —— 抽象类练习 | 第 2 级
—— Level 2,Lesson 6。
任务 1 —— 抽象宠物类
让 Pet 类设为 abstract。
答:
package zh.codegym.task.task12.task1211;
/*
抽象宠物类
*/
public class Solution {
public static void main(String[] args) {
}
public static abstract class Pet {
public String getName() {
return "我是一只小猫";
}
}
}
任务 2 —— “修复代码”,第 1 部分
更正代码,让程序可以编译。
提示:
getChild 方法必须保持为 abstract。
答:
package zh.codegym.task.task12.task1212;
/*
“修复代码”,第 1 部分
*/
public class Solution {
public static void main(String[] args) {
}
public static abstract class Pet {
public String getName() {
return "我是一只小猫";
}
public abstract Pet getChild();
}
}
任务 3 —— “修复代码”,第 2 部分
更正代码,让程序可以编译。
答:
package zh.codegym.task.task12.task1213;
/*
“修复代码”,第 2 部分
*/
public class Solution {
public static void main(String[] args) {
}
public static abstract class Pet {
public String getName() {
return "我是一只小猫";
}
public abstract Pet getChild();
}
}
任务 4 —— 牛也是动物
让 Cow 类继承 Animal。
实现 Cow 类的缺失方法。
答:
package zh.codegym.task.task12.task1214;
/*
牛也是动物
*/
public class Solution {
public static void main(String[] args) {
}
public static abstract class Animal {
public abstract String getName();
}
public static class Cow extends Animal {
@Override
public String getName() {
return "Cow";
}
}
}
任务 5 —— 猫不应该是抽象的!
让 Cat 和 Dog 类继承 Pet。
实现缺失的方法。
Cat 和 Dog 类不得为 abstract。
答:
package zh.codegym.task.task12.task1215;
/*
猫不应该是抽象的!
*/
public class Solution {
public static void main(String[] args) {
}
public static abstract class Pet {
public abstract String getName();
public abstract Pet getChild();
}
public static class Cat extends Pet {
@Override
public String getName() {
return "猫";
}
@Override
public Pet getChild() {
return new Cat();
}
}
public static class Dog extends Pet {
@Override
public String getName() {
return "狗";
}
@Override
public Pet getChild() {
return new Dog();
}
}
}
这篇博客提供了多个关于Java整数类型转换的练习任务,包括强制转换、运算符使用等,旨在帮助读者巩固Java基础。此外,还包括关于继承、封装、多态、重载方法和抽象类的实践题目,适合初学者提升编程技能。

5956

被折叠的 条评论
为什么被折叠?



