1.判断三角形类型
代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
float a,b,c;
a=in.nextFloat();
b=in.nextFloat();
c=in.nextFloat();
if(a>200||a<1||b>200||b<1||c>200||c<1)
System.out.print("Wrong Format"); //数据非法
else if(a+b<=c||a+c<=b||b+c<=a)
System.out.print("Not a triangle");//不能构成三角形
else if(a==b||a==c||b==c)
{
if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b||a*a+b*b-c*c<0.00001||a*a+c*c-b*b<0.00001||b*b+c*c-a*a<0.00001)
System.out.print("Isosceles right-angled triangle");//等腰直角三角形
else if(a==b&&b==c)
System.out.print("Equilateral triangle");//等边三角形
// else if(a*a+b*b-c*c<0.00001||a*a+c*c-b*b<0.00001||b*b+c*c-a*a<0.00001)
// System.out.print("Isosceles triangle");//等腰三角形
else
System.out.print("Isosceles triangle");//等腰三角形
}
else if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b)
System.out.print("Right-angled triangle");//普通直角三角形
else if(a+b>c&&a+c>b&&b+c>a)
System.out.print("General triangle");//普通三角形
}
}
分析:根据三角形三边的长度来判断是什么三角形,存在勾股定理的是直角三角形,两边相等是等腰,三边相等是等边,还有等腰直角三角形也是如此,其他均为普通三角形。
改进:也同样可以用这样的方法来判断锐角、直角以及钝角三角形,而不只是等腰、等边和直角。
心得:这是学习Java的第一次作业,锻炼了我的思维能力,以及学会Java里的一些基本语法。
2. 求下一天
代码:
import java.util.Scanner;
public class Main {
public static boolean Leap(int year)
{
if((year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
public static void next(int year,int month,int day)
{
switch (month)
{
case 1:
if(day==31)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 2:
if(Leap(year)&&day==29)
{
month++;
day=1;
}
if(!Leap(year)&&day==28)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 3:
if(day==31)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 4:
if(day==30)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 5:
if(day==31)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 6:
if(day==30)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 7:
if(day==31)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 8:
if(day==31)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 9:
if(day==30)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 10:
if(day==31)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 11:
if(day==30)
{
month++;
day=1;
}
else
{
day++;
}
break;
case 12:
if(day==31)
{
year++;
month=1;
day=1;
}
else
{
day++;
}
break;
}
System.out.println("Next date is:"+year+"-" + month + "-" + day);
}
public static boolean checkInputValidity(int year,int month,int day)
{
if(year<1820||year>2020||month<1||month>12||day<1||day>31)
{
return false;
}
else
{
if(!Leap(year)&&month==2&&day>28)
{
return false;
}
else if(Leap(year)&&month==2&&day>29)
{
return false;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31)
{
return false;
}
else if((month==4||month==6||month==9||month==11)&&day>30)
{
return false;
}
else
return true;
}
}
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int year=input.nextInt();
int month=input.nextInt();
int day=input.nextInt();
boolean d=checkInputValidity(year,month,day);
if(d==false)
{
System.out.println("Wrong Format");
}
else if(d==true)
{
next(year,month,day);
}
}
}
分析:求下一天的问题,之前在学c语言的时候学过,有许多限制,比如闰年、二月份等,所以需要把这些都要考虑进去,这样便大大增加了难度,根据这些,代码中运用switch语句来执行,毕竟每个月的天数不同。
改进:代码长度过长,可以精简一些。
心得:此次作业加强了我的Java语言能力以及逻辑思维,算法也有待改进。
3.求前N天
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int year, month, day, a;
year = in.nextInt();
month = in.nextInt();
day = in.nextInt();
a = in.nextInt();
int M[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 || a < -10 || a > 10)
System.out.println("Wrong Format");
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
M[2] = 29;
else if (day - a >= 1 && day - a <= M[month])
System.out.printf("%d days ago is:%d-%d-%d\n", a, year, month, day - a);
else {
if (day - a < 1) {
int temp = 0 - (day - a);
if (month - 1 < 1) {
year -= 1;
month = 13;
}
System.out.printf("%d days ago is:%d-%d-%d\n", a, year, month - 1, M[month - 1] - temp);
} else {
int temp = day - a - M[month];
if (month > 11) {
year += 1;
month = 0;
}
System.out.printf("%d days ago is:%d-%d-%d\n", a, year, month + 1, temp);
}
}
}
}
分析:和之前的求下一天一样,有着诸多限制条件,基本与上题相似。
改进:本题运用数组的知识,而没有用到之前的switch语句,提高了代码的精简度,避免冗余。
心得:这次作业,给了我另一种思路,而不仅仅是拘泥于一种,算法有很多,但越精简越好。
4. 定义日期类
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
//Date date=new Date();
Scanner input = new Scanner(System.in);
int year=input.nextInt();
int month=input.nextInt();
int day=input.nextInt();
//date.setYear(year);
//date.setMonth(month);
//date.setDay(day);date.getDay();
//date.getMonth();
//date.getYear();
Date date=new Date(year,month,day);
date.getNextDay();
}
}
class Date{
private int year;
private int month;
private int day;
int[] mon_maxnum = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date(){}
Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public int getYear(){
return year;
}
public void setYear(int year){
if(year<1900||year>2000)
System.out.println("Date Format is Wrong");
else
this.year=year;
}
public int getMonth(){
return month;
}
public void setMonth(int month){
if(month<1||month>12)
System.out.println("Date Format is Wrong");
else
this.month=month;
}
public int getDay(){
return day;
}
public void setDay(int day){
if(day<1||day>31)
System.out.println("Date Format is Wrong");
else
this.day=day;
}
public boolean isLeapYear(int year){
if((year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){
return true;
}
else{
return false;
}
}
public boolean checkInputValidity()
{
int[] mon_maxnum = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
mon_maxnum[2] = 29;
boolean checkInputValidity = (year >= 1900 && year <= 2000 && month > 0 && month <= 12 && day <= mon_maxnum[month] && day > 0);
return checkInputValidity;
}
public void getNextDay(){
int a = 0, b = 0,c = 0;
if (checkInputValidity()) {
if (month == 12) {
if (day == 31) {
a = year + 1;
b = 1;
c = 1;
} else {
a = year;
b = month;
c = day + 1;
}
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) {
if (day == 31) {
a = year;
b = month + 1;
c = 1;
} else {
a = year;
b = month;
c = day + 1;
}
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day == 30) {
a = year;
b = month + 1;
c = 1;
} else {
a = year;
b = month;
c = day + 1;
}
}
if (month == 2) {
if (isLeapYear(year)) {
if (day == 29) {
a = year;
b = month + 1;
c = 1;
} else {
a = year;
b = month;
c = day + 1;
}
} else {
if (day == 28) {
a = year;
b = month + 1;
c = 1;
} else {
a = year;
b = month;
c = day + 1;
}
}
}
System.out.println("Next day is:" + a + "-" + b + "-" + c);
}
else {
System.out.println("Date Format is Wrong");
}
}
}
分析:这是一次有关Java类的题目,包含了对类的封装,大量的内容都在创建的Date类中,而不是主函数,同样也是要注意关于闰年以及特殊月份的问题。
类图:
心得:这是一次有关类的作业,和之前的作业已有不同,用到了Java面向对象的思维,对于以后的面向对象的思维提供了一些思路。
5. 一元多项式求导(类设计)
代码:
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
String str=new String(input.nextLine());
CheckD cd=new CheckD(str);
if(!cd.Check()) {
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println("Wrong Format");
}
}
class CheckD{
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public CheckD(String str) {
setStr(str);
}
public boolean Check() {
setStr(getStr().replace(" ", ""));
if(getStr().equals("")) {
return false;
}
Matcher m=Pattern.compile("((\\+|\\-)?0\\*x)|(x\\^(\\+|\\-)?0)").matcher(getStr());
if(m.find()) {
return false;
}
if(!Pattern.matches("(((\\+|\\-)?\\d+\\*x\\^(\\+|\\-)?\\d+)|((\\+|\\-)?\\d+\\*x)|((\\+|\\-)?x\\^(\\+|\\-)?\\d+)|((\\+|\\-)?(\\d+|x)))+", getStr())) {
return false;
}
return true;
}
}
abstract class Term{
abstract public String getD();
}
class ConstantTerm extends Term{
private BigInteger constant;
public ConstantTerm(String constant) {
setConstant(new BigInteger(constant));
}
public String getD() {
return null;
}
public BigInteger getConstant() {
return constant;
}
public void setConstant(BigInteger constant) {
this.constant = constant;
}
}
class PowTerm extends Term{
private BigInteger constant;
private BigInteger pow;
public PowTerm(String term) {
Matcher m=Pattern.compile("([\\*]?x[\\^]?)|([+-]?\\d*)").matcher(term);
String g[]=new String[4];
int i=0;
while(m.find()) {
g[i]=new String(m.group());
i++;
}
if(g[0].equals("+")||g[0].equals(""))
setConstant(new BigInteger("1"));
else if(g[0].equals("-"))
setConstant(new BigInteger("-1"));
else
setConstant(new BigInteger(g[0]));
if(g[2].equals("+")||g[2].equals(""))
setPow(new BigInteger("1"));
else if(g[2].equals("-"))
setPow(new BigInteger("-1"));
else
setPow(new BigInteger(g[2]));
}
public BigInteger getConstant() {
return constant;
}
public void setConstant(BigInteger constant) {
this.constant = constant;
}
public BigInteger getPow() {
return pow;
}
public void setPow(BigInteger pow) {
this.pow = pow;
}
public String getD() {
return null;
}
}
分析:这是一道关于求导的题目,同样用到类,而题目中并没有给出类的设计思路,都要靠自己来进行类设计,大大增加了难度
心得:本次实验的难度较大,刚开始没有任何思路,最后是问了学长,才打开了思路,但还没有全部作对,还有待提高。
6.总结:在这一系列的实验中,我基本掌握了Java的编程规则、知识要点和一些小技巧,特别是对面向对象的编程思想和风格有了进一步的认识和体会。同时,因正确的编出程序而带来的成就感让我对编程更加感兴趣。编程的经验应该是多了很多,发现JAVA虽然看起来比C或C++要容易一点,但是由于它自身带有相当多的方法定义,也同样增加了Java的复杂程度,像求导那道题目,跟以前学到的有了很大不同,不仅仅需要语法,更需要的是算法,如何更加精简,更加通俗,从这方面出发,对我的帮助一定很大。
原文:https://www.cnblogs.com/gojosang/p/14907731.html