java例题

  1. 定义两个成员方法,一个成员方法实现为此成员变量赋值,另一个成员方法获取这个成员变量的值,保证其他类继承该类时能获取该类的成员变量的值
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;		
		}		
		}
	}
  1. 分别截取字符串str1和字符串str2中的部内容,如果截取后的两个子串相同(不区分大小写)会输出“两个子串相同”,否则输出“两个子串并不相同”
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]+"");
  }
 }
  1. 编写一个矩形类,将长宽作为矩形的属性,在构造方法中将长,宽初始化,定义一个成员方法求此矩形的面积
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());
     
   }
  }
}
  1. 编写Java程序将数组的最小数输出
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]);    
           }
          } 

  1. 编写Java程序,将二维数组中的行列互调显示出来显示出的结果。
    例如:{1 2 3}{4 5 6}{7 8 9}
    结果为{1 4 7}{2 5 8}{3 6 9 }
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();
      }
  }
  }
  1. 定义一个时间类,其中有三个私有整型成员时hour分minute秒second, 定义构造方法以方便创建对象时初始化成员变量。 再定义diaplay方法用于将时间信息打印出来。 定义成员方法分别对时、分、秒进行加减运算,保证运算后时间为真实时间
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);
         }
       }
  1. 定义一个网络用户走。的参数输入,其中用户ID和用户密码时必须的, 募处理的信息有用户TD、用户密码、cnail地址,在建立类的实例时,把以上三个信息都作为构造函数缺省的email地址是用户ID加上字符串”@
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);
 }
 }
 

以上均为本人课上或课下老师布置的题目,如有错误请指正

你可能感兴趣的:(java)