日常20190917

这是我的第一个博客吧,就先讲讲我今天赶的五个Java程序吧

第一次搞markdown稍微花了点力气,主要是csdn的在线编辑器实在有点小坑,现在用MarkdownPad先写好再导入到csdn上,再看看有什么要改的地方

一、图形抽象类

1)定义shape抽象类,包含求面积和求周长的方法。
2)定义Circle类、Rectangle类、Square类。
3)要求Circle类和Rectangle类继承shape类,Square类继承Rectangle类。 运行时,让用户选择输入什么图形,然后输入相应数据,求出该图形的面积和周长。


abstract class shape{
	abstract double area();//抽象方法
	abstract double circum();
}

class rectangle extends shape{
	float length;//长
	float width;//宽
	
	rectangle(float length,float width){//构造方法
		this.length = length;
		this.width = width;}
	
	double area() {
		return length * width;}

	double circum() {
		return 2 * (length + width);}
}

class square extends rectangle{//方形继承长方形
	square(float sl) {//直接调用父类的构造方法
		super(sl, sl);
	}
	
}

public class figure {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int choice;
		float r, l, w, sl;
		System.out.println("请输入您想要输入哪种图形:\n1. 圆形 2. 长方形 3. 正方形");
		choice = sc.nextInt();
		//圆形略
		else if(choice == 2) {
			System.out.println("请输入长方形的长和宽:");
			l = sc.nextFloat(); w = sc.nextFloat();
			shape rec = new rectangle(l,w);
			System.out.println("面积是"+rec.area()+"周长是"+rec.circum());
		}
		else if(choice == 3) {
			System.out.println("请输入正方形的边长:");
			sl = sc.nextFloat();
			shape sq = new square(sl);
			System.out.println("面积是"+sq.area()+"周长是"+sq.circum());
		}
		else
			System.out.println("输入错误!");
	}

}

注意抽象类的子类方法是如何调用的
shape rec = new rectangle(l,w);
System.out.println(“面积是”+rec.area()+“周长是”+rec.circum());

二、接口练习

1)定义shape接口,包含求面积和求周长的方法。
2)定义Circle类、Rectangle类、Square类。
3)要求Circle类和Rectangle类实现shape接口,Square类继承Rectangle类。 运行时,让用户选择输入什么图形,然后输入相应数据,求出该图形的面积和周长。

interface Shape{
	double area();
	double circum();
}

class Circle implements Shape{//略
}

class Rectangle implements Shape{
	float length;
	float width;
	
	Rectangle(float length, float width){
		this.length = length;
		this.width = width;}
	
	public double area() {
		return length * width;}

	public double circum() {
		return 2 * (length + width);}
}

class Square extends Rectangle{//略
}
//其余略

三、应用练习(三个程序)

1.输入一个字符串,判断其中字母、数字、其他字符的个数。(String类)

public class string {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str;
		int Char = 0, num = 0, others = 0;
		System.out.println("请输入要测试的字符串:");
		str = sc.nextLine();
		char[] S = str.toCharArray();
		for(int i = 0; i < str.length(); i++) {
			if(S[i] <= '9' && S[i] >= '0')
				num++;
			else if(S[i] <= 'Z' && S[i] >= 'A')
				Char++;
			else if(S[i] <= 'z' && S[i] >= 'a')
				Char++;
			else
				others++;
		}
		System.out.println("该字符串中,有字母" +Char+ "个,
		数字" +num+ "个,其他字符"+others+"个");
	}

}

==char[] S = str.toCharArray();==将字符串str转为字符数组S
==str.length()==计算字符串str的长度

2.搜索字符串S中是否包含字符串s1,如果包含,则替换为s2。(StringBuffer类)

public class StringBufferDown {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String S,S1,S2;
		int m = 0,start = 0,end = 0;
		
		System.out.println("请输入原字符串S:");
		S = sc.nextLine();
		System.out.println("请输入你要替换的字符串s1:");
		S1 = sc.nextLine();
		System.out.println("请输入你要加入的字符串s2:");
		S2 = sc.nextLine();
		
		char[] s = S.toCharArray();
		char[] s1 = S1.toCharArray();
		for(int n = 0;n < S.length();n++) {
			if(m == S1.length())
				break;
			else if(s[n] == s1[m]) {
				m++;
				end = n;
			}
			else if(s[n] != s1[m]) {
				m = 0;
				end = 0;
			}
		}
		start = end - S1.length();
		StringBuffer str = new StringBuffer(S);
		str.replace(start+1,end+1,S2);
		if(end != 0)
			System.out.println(
			"新字符串为"+S.replace(S1, S2));
	}

}

使用前需要声明\\StringBuffer str = new StringBuffer(S);
将字符串s中从第start个字符开始到第end个字符串结束的字符都去掉换为字符串s2\\s.replace(start,end,s1)

3.改写汽车租赁程序,增加一个汽车类(抽象类),轿车类、卡车类等继承该类。

这个程序我之前写的功能多了一两个了,要改有点麻烦,就直接传了吧

另外这其实是我18号写的

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值