package test;
public class test1 {
public class dog{
private int age;
public void setAge(int age){
this.age = age;
}
public int getAge(int age){
return this.age;
}
}
}
package test;
public class test2 {
public static void main(String[] args) {
String str1="hello";
String str2="welcome";
String substr1=str1.substring(0,3);
String substr2=str2.substring(0,3);
if (substr1.equalsIgnoreCase(substr2))
{
System.out.println("两个子串相同");
}
else{
System.out.println("两个子串并不相同");
}
}
}
3.编写Java程序,创建数组arr1和arr2,将数组arr1中索引位置是0~3中的元素复制到数组arr2中,最后将数组arr1和arr2中的元素输出
package test;
import java.util.Arrays;
public class test3 {
public static void main(String[] args)
{
int[] arr1= {1,2,3,4,5};
int[] arr2;
arr2=Arrays.copyOfRange(arr1,0, 4);
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
for(int i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]+"");
}
System.out.println();
for(int j=0;j<arr2.length;j++)
{
System.out.println(arr2[j]+"");
}
}
package test;
public class test4 {
public class rectangle {
private int length;
private int width;
public rectangle(int length,int width) {
this.length = length;
this.width = width;
}
public int getArea() {
return this.length*this.width;
}
public void main(String[] args){
rectangle rect = new rectangle(6,4);
System.out.println(rect.getArea());
}
}
}
package test;
public class test5 {
public static void main(String[] args) {
int arr[] = {12, 34, 13, 24, 999, 9};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("数组中的最小数为:" + arr[0]);
}
}
package test;
public class test6 {
public static void main(String[] args) {
int arr[][]=new int[][] {{1,2,3},{4,5,6},{7,8,9}};
test2 sorter=new test2();
sorter.sort(arr);
}
public void sort(int[][] arr) {
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
int temp =arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
System.out.println("test2 array:");
ShowArray(arr);
}
public void ShowArray(int[][] arr) {
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
System.out.println("\t"+arr[j][i]);
}
System.out.println();
}
}
}
package test;
class clock {
int hour,min,sec;
clock(int h,int m,int s){
hour=h;
min=m;
sec=s;
}
clock (){
}
void show()
{
System.out.printf("时间 %d:%d:%d",hour,min,sec);
System.out.println();
}
}
public class test2 {
public static void main(String []args){
clock c1=new clock(4,20,9);
clock c2=new clock();
clock c3=new clock(14,2,9);
c2.hour =2;
c2.min =3;
c2.sec =4;
c1.show();
c2.show();
c3.show();
}
}
9.构造方法:编写Java程序,用于显示人的姓名和年龄。定义一个人类(Person), 该类中应该有两个私有属性,姓名(name)和年龄(age)。定义构造方法, 用来初始化数据成员。再定义显示(display)方法, 将姓名和年龄打印出来。在main方法中创建人类的实例,然后将信息显示。
package test;
class Person {
String name;
int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
public class test2 {
public static void main(String[] args) {
Person Xiaohong=new Person("小红",18);
System.out.println(Xiaohong.name+"\n"+Xiaohong.age);
}
}
package test;
class Net{
int id;
int mima;
String eamil;
Net(int id,int mima){
System.out.println("用户名为:"+id+"密码为:"+mima+" \n 地址为:"+id+"@gameschool.com");
}
}
public class test2 {
public static void main(String[]args){
Net N1=new Net(1500,10);
}
}
以上均为本人课上或课下老师布置的题目,如有错误请指正