JAVA期末篇

JAVA期末篇,第1张

JAVA期末篇

Fuxi内容:①类创对象,②final,static方法的调用,③私有封装,构造器,修改器,  ④抽象类,接口

package review;

public class Fuxi {//内容:①类创对象,②final,static方法的调用,③私有封装,构造器,修改器,  ④抽象类,接口
	public  static void main(String[] args) {    
		//Function test = new Function(); 
		Fuxi.test1();//法二调用测试!
		      
	}

	public static void test1() {   
		int a[] = {1,2,3,4,5};
		Function.reserve(a);
		Function.printarray(a);//静态方法的两种调用,多了个类名.调用
		Function f = new Function();
		f.printarray(a);//静态方法的两种调用
		System.out.println(f.Chinese);
		Function f1 = new Function("个一百",1);
		f1.setMoney(6);
		System.out.println(f1.money + f1.getName() );
	}
}
//接口多继承性 
 class Function extends a implements b ,c{  //abstract class Function implements b就可以不实现抽象方法但不能造对象了,接口不能定义构造器,也不能创对象;  

	 static final String Chinese = "中国人" ;
	private String name;
	int money;
	public Function() {
		
	}
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	
	public Function(String name,int money) {
		this.money = money;
		this.name = name;
			//name = "zg";  局部变量赋值罢了
	}
	public static void reserve(int a[]){ //不加返回值变构造器,没有静态构造器
		for(int i = 0;i < a.length/2;i++) {//i < a.length两次换
			int temp = a[i];
			a[i] = a[a.length - 1 - i];
			a[a.length - 1 - i] = temp;
		}
		
		return ;
	}
	 static void printarray(int a[]) {  //静态方法类名调用,或创建对象
		for(int i = 0;i < a.length;i++) {    
			System.out.print(a[i] +" ");
		}
		System.out.println();
		
	}
	 
	@Override
	public void usb() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void bb() {
		// TODO Auto-generated method stub
		
	}
	
}

abstract class a{
	a(){//可以构造器
		
	}
	
}

interface b{//接口(静态常量、抽象方法,)
	public abstract void usb();
	void bb();//省略public abstract
	//public b(){};  不能定义构造器,不能创对象, 借用最终子类(非抽象)实现方法,子类对象调用方法
}



interface c{
	
	
}




Fuxi2内容:

① equals

②包装类

③toString()打印对象地址值显示,文件.类名@哈希计算值,可重写,Date,String库中就已重写 

package review;



public class Fuxi2 {//① equals  ---- ②包装类  ----  ③toString()打印对象地址值显示,文件.类名@哈希计算值,可重写,Date,String就已重写 
	//String的toString是打印字符串
	public static void main(String[] args) {//
		Circle text = new Circle(); 
		text.test1();
	}
}


class Circle{
	double radius;

	public  Circle() {
	}
	public Circle(double radius) {
		this.radius = radius;
	}
		public	void test1() {
			Circle c1 = new Circle(1.1);
			Circle c2 = new Circle(1.1); 
			System.out.println(c1.equals(c2));
			int t = 1;
			char ch1 = 'a';
			Character c = ch1;
			Integer n = t;//自动装箱     转换包装类 做 如Obeject类的类型参数
			int m = n;//自动拆箱     //Character , Integer  ,其他为大写如Boolean , Float   //此处未与String转换
			String s = "123";//除了Boolean ,要纯粹"123"转Int 或 abc ( "123a"会throw NumberFormatException ) 
	    
			boolean a = Boolean.parseBoolean(s);  //字符串转基本数据类型  ( 包装类.parse包装类() )
			int sint = Integer.parseInt(s);
			
			boolean b =new Boolean("true");
			boolean b1 =new Boolean(true);
			boolean b2 =new Boolean("true123");  //基本数据类型转字符串
			Fuxi2 ff = new  Fuxi2();
			System.out.println( ff.toString() );
			
			System.out.println(String.valueOf(t));//基本数据类型转字符串     调用String重载的valueOf()
			String ssString = t + ""; //基本数据类型转字符串    运算连接
			//throw NumberFormatException
			//int b = Integer.parseInt(s); 		
			//法二 
			
			Boolean B = b;//自动装箱
			
			System.out.println( a );
			System.out.println(sint );
			System.out.println( b + " " + b1 +b2);
			
		} 
	 
	

@Override   
public boolean equals(Object obj) {  //比较内容
	if (this == obj)
		return true;
	if(obj instanceof Circle) {
	Circle other = (Circle) obj;
	return true;
	}
	return false;
	
}

}













十六进制 转换 十进制

package review;
import java.lang.System;
public class a1 {
public static void main(String[] args) {
	
}





}



class huona{
	public static int hx(String hex) {
		int devalue = 0;
		for(int i = 0;i < hex.length();i++) {
			char hexchar = hex.charAt(i);
			devalue = devalue*16 + hxchar(hexchar);
			
		}
		return devalue;
	}
	public static int hxchar(char ch) {
		if(ch>='A' &&ch <= 'F') {
			return (ch-'A'+ 10);
		}else return (ch-'0');//(int)ch
	}
	
}

数组

package review;
import java.util.Scanner;
public class b1 {
	public static void main(String[] args) {
		double a = 1;
		double []arr[] = {{1,2,3},{1,2,3,4},{1,3,5,7,9}};
		double []arr2[] = new double[10][];//行必写,可省略列
		double []arr3[] = new double[][]{{1},{2},{3},{2,3}};//写法三
		double []arr4 = new double[5];
		
		System.out.println( arr3[0][0]);
	}
}


class Cylinder{
	private double r = Math.PI;
	private double h = 1;
	
	public double getR() {
		return r;
	}
	public void setR(double r) {
		this.r = r;
	}
	public double getH() {
		return h;
	}
	public void setH(double h) {
		this.h = h;
	}
}

VowelQuestion

package review;

import java.util.Scanner;

public class VowelQuestion {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input= new Scanner(System.in);
		String word=input.next();
		System.out.println(getFirstVowel(word));
	}
	public static char getFirstVowel(String word) {
		char ch;
		for(int i=0;i 

大作业

package xuexitong;
public class bigwork {
	public static void main(String[] args){
		Cylinder C1 = new Cylinder(1,1);
		Cylinder C2 = new Cylinder(1,1);
		C2.setRadius(6);
		System.out.println(C1.equals(C2)); 
		
	} 
}
abstract class Shape {
    abstract double getArea();
}
class Circle extends Shape{    
	 double radius;
	public  Circle() {}
	public Circle(double radius) {
		this.radius = radius;

	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	@Override
	 public double getArea() {
		return Math.PI*radius*radius;     
	}
}
class Cylinder extends Circle{
	double height;
	public Cylinder() {}
	public  Cylinder(double radius,double height) {
		this.height = height;
		this.radius = radius;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	 @Override
	public double getArea() {
		  return 2*Math.PI*radius+2*radius*Math.PI*height;
		 }
	 @Override
	 public boolean equals(Object object){//重写比较内容
		 if(this == object) {
			 return true;
		 }
	        if (object instanceof Circle){//判断obj是否是Circle的实例对象,是就可以向下转型
	            Circle circle=(Circle)object;
	            return this.radius==circle.radius; 
	        }
	        else  return false;
	       
	 }

}











大作业测试

package xuexitong;

public class testbigwork {
	public static void main(String[] args) {
		Shape[] shape={new Circle(2.3),new Cylinder(3.6,5.8)};
        for(Shape object:shape){    	
        	 System.out.println(object.getArea());
        }
}
}

结构体类型数组

package xuexitong;

public class testUsb {
	    public static void main(String[] args){
	        Usb []usb={new UsbMouse(),new UsbKeyborder()};
	        for(Usb object:usb){
	            object.work();
	        }
	    }
	
}

加密

package xuexitong;  
import java.util.Scanner;

public class teacheradvicejiami {
	public static void main(String[] args) {
		
		 Scanner input = new Scanner(System.in); 
		 String s = input.nextLine(); 
				 
		 password(s);
		 passwordflagmod(s);

	}
	public static void password(String s) {   //不用数组 ,用String字符串类型和charAt(i)逐个搜索
		
	                                                 //A的加密: A = (char)((flag(A) + 3)%26)
		for(int n = 0 ;n 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存