一、描述
请你在 “// write your code here” 的下方编写代码,以获取字符串数组 stringArray
中所有字符串长度的总和。
如果什么也不输入,系统会默认判定输入的是一个空数组,由于该数组内没有字符串元素,你需要判定该数组内字符串的长度为 0。如果该数组内存在空字符串,则该字符串的长度为 0。
样例
评测系统会将整个项目的代码编译为一个可执行的 Main
程序,并按照这样的方式执行你的代码 Main <str1> <str2>...
(数组元素之间用空格隔开)。
当执行 Main Liubei guanyu zhangfei
的时候,你的代码应该输出:
20
当没有任何数据输入时,你的代码应该输出:
0
因为输入数据中,数组内的元素是空字符串,所以字符串长度是 0。
public class Solution {
public int getArrayStringLength(String[] stringArray) {
// write your code here
int sum=0;
if(stringArray.length==0)
return 0;
for(int i=0;i<stringArray.length;i++){
sum+=stringArray[i].length();
}
return sum;
}
}
二、描述
在本题中,Son
类继承 Father
类的属性和方法,请你在 Son
类和 Main
类的 // write your code here
下方编写合适的代码,通过继承实现 Son
类的创建和打印。
要求一:在 Main
文件中创建一个名为 son1
的 Son
对象,并使用 introduction
方法打印数据。
要求二:在 Main
文件中创建一个名为 son2
的 Son
对象,给 name
属性赋值为 “Zhuge Zhan”,给 id
属性赋值为 2,并使用 introduction
方法打印数据。
注意对象的创建格式创建对象时,会调用对应类的构造方法,请注意构造方法的书写格式
样例
评测机会将你的代码编译为一个可执行文件 Main
并执行,你可以在 Main.java
中了解更多详情。
这道题无需任何数据的输入,你的代码在执行之后应该输出
Hello, my name is Zhuge Liang, my id is 1.
Hello, my name is Zhuge Zhan, my id is 2.
public class Father {
private String name = "Zhuge Liang";
private int id = 1;
public Father() {
}
/**
*
* @param name: Father's name
* @param id: Father's id
*/
public Father(String name, int id) {
this.name = name;
this.id = id;
}
public void introduction() {
System.out.println("Hello, my name is " + name + ", my id is " + id +
".");
}
}
public class Son extends Father {
// write your code here
String name;
int id;
public Son(){}
public Son(String name,int id){
super(name,id);
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
// write your code here
Father son1=new Son();
Father son2=new Son("Zhuge Zhan",2);
son1.introduction();
son2.introduction();
}
}