前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java面向对象1(A~F)

Java面向对象1(A~F)

作者头像
Lokinli
发布2023-03-09 16:50:19
1950
发布2023-03-09 16:50:19
举报
文章被收录于专栏:以终为始以终为始

QWQ请假一节课,错过一章内容,只能求助qsh了。

C/C++训练1---最大公约数与最小公倍数(SDUT 1131)

代码语言:javascript
复制
import java.util.*;

class Number {
	int a, b;

	Number(int n, int m) {
		a = n;
		b = m;
	}

	int getGcd() {
		int n = a, m = b;
		while (m > 0) {
			int x = n;
			n = m;
			m = x % m;
		}
		return n;
	}

	int getLcm() {
		int x = getGcd();
		return a * b / x;
	}

	void Print() {
		System.out.println(getGcd() + "\n" + getLcm());
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Number num = new Number(sc.nextInt(), sc.nextInt());
		num.Print();
		sc.close();
	}
}

C/C++经典程序训练3---模拟计算器

代码语言:javascript
复制
import java.util.Scanner;
class Number
{
	int a, b;
	String c;
	Number(int n, int m, String k)
	{
		a = n;
		b = m;
		c = k;
	}
	int getAns()
	{
		int ans = 0;
		if(c.equals("+"))ans = a + b;
		else if(c.equals("-"))ans = a - b;
		else if(c.equals("*"))ans = a * b;
		else ans = a / b;
		return ans;
	}
	void Print()
	{
		System.out.println(getAns());
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a, b;
		String c;
		a = sc.nextInt();
		b = sc.nextInt();
		sc.nextLine();
		c = sc.nextLine();
		Number p = new Number(a, b, c);
		p.Print();
		sc.close();
	}
}

面向对象程序设计上机练习一(函数重载)(SDUT 1140)

代码语言:javascript
复制
import java.util.Scanner;
class Max
{
	
	 static int getMax(int a[])
	{
		int ans = -1;
		for(int i = 0; i < 5; i ++) if(a[i] > ans) ans = a[i];
		return ans;
	}
	 static float getMax(float b[])
	{
		float ans = -1;
		for(int i = 0; i < 5; i ++) if(b[i] > ans) ans = b[i];
		return ans;
	}
	 static  long  getMax(long  c[])
	{
		long ans = -1;
		for(int i = 0; i < 5; i ++) if(c[i] > ans) ans = c[i];
		return ans;
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[10];
		float b[] = new float[10];
		long c[] = new long[10];
		for(int i = 0; i < 5; i ++) a[i] = sc.nextInt();
		for(int i = 0; i < 5; i ++) b[i] = sc.nextFloat();
		for(int i = 0; i < 5; i ++) c[i] = sc.nextLong();
		Max max = new Max();
		System.out.print(max.getMax(a)+"\n"+max.getMax(b)+"\n"+max.getMax(c)+"\n");
	}
}

D    圆的面积 (SDUT 1588)

代码语言:javascript
复制
import java.util.Scanner;
import java.text.DecimalFormat;

class Sum {
	double x;

	Sum(double n) {
		x = n;
	}

	double getAns() {
		return (x * x * 3.141592653);
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		DecimalFormat df = new DecimalFormat(".00");
		int t;
		t = sc.nextInt();
		for (int i = 1; i <= t; i++) {
			Sum n = new Sum(sc.nextDouble());
			System.out.println("Case" + " " + i + ":" + " " + df.format(n.getAns()));
		}
	}
}

E    正方形面积(SDUT 2101)

代码语言:javascript
复制
import java.util.Scanner;
import java.text.DecimalFormat;

class Sum {
	long x;

	Sum(long n) {
		x = n;
	}

	long getAns() {
		return (x * x);
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// df = new DecimalFormat(".00");
		while (sc.hasNext()) {
			Sum n = new Sum(sc.nextLong());
			System.out.println(n.getAns());
		}
	}
}

F    回文时间 (SDUT 2174)

代码语言:javascript
复制
import java.util.*;
class Time{
	String s;
	int a,b,c,d;
	Time(String s){
		this.s = s;
		a = s.charAt(0) - '0';
		b = s.charAt(1) - '0';
		c = s.charAt(3) - '0';
		d = s.charAt(4) - '0';
	}
	void getAns() {
		if(a == d && b == c)d ++;
		while(a != d || b != c) {
			d ++;
			if(d == 10) {
				d = 0;
				c ++;
			}
			if(c == 6) {
				c = 0;
				b ++;
			}
			if(b == 10) {
				b = 0;
				a ++;
			}
			if(b + a * 10 >= 24) {
				a = 0;
				b = 0;
			}
		}
		System.out.println(a +""+ b + ":" + c +""+ d);
	}
}
public class Main{
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		String s;
		while(sc.hasNext()) {
			s = sc.nextLine();
			Time p = new Time(s);
			p.getAns();
		}
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • D    圆的面积 (SDUT 1588)
  • E    正方形面积(SDUT 2101)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档