1.public class Date
{
int year;
int month;
int day;
public void setDate(int y, int m, int d){
year=y;
month=m;
day=d;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public boolean isLeapYear()
{
boolean isLeap=true;
if(this.year%100==0)
{
if(this.year%400==0)
isLeap=true;
else isLeap=false;
return isLeap;
}
else
{
if(this.year%4==0&&this.year%100!=0)
isLeap=true;
else isLeap=false;
return isLeap;
}
}
public void display(){
System.out.println("当前日期是"+this.getYear()+"年"+this.getMonth()+"月"+this.getDay()+"日");
}
}
public class zuoye2_1{
public static void main(String [] args){
Date date = new Date();
date.setDate(1900, 1, 1);
boolean isLeap=date.isLeapYear();
if(isLeap)
System.out.println(date.getYear()+"是闰年");
else
System.out.println(date.getYear()+"不是闰年");
date.display();
}
}
2.public class Triangle {
private double x, y, a;
public Triangle(int xx, int yy, int aa)
x = xx;
y = yy;
a = aa;
}
public double area() {
return x * y * Math.sin(a) / 2;
}
public void changeAng(double aa) {
a = aa;
}
public String toString() {
return "两边长 " + x + " 和 " + y +
" ,夹角为 " + a + " 的面积为 " + area();
}
static public void main(String args[]) {
Triangle a = new Triangle(3, 4, Math.PI / 3);
Triangle b = new Triangle(1, 1, Math.PI / 4);
System.out.println(a);
System.out.println(b);
b.changeAng(Math.PI / 2);
System.out.println(b);
}
}
public class zuoye2_3 {
private double a;
private double b;
private double c;
private double[] d;
public String[] i;
public void setValue( int aa, int bb, int cc) {
a = aa;
b = bb;
c = cc;
this.d = new double[2];
this.i = new String[2];
}
public boolean canGetValue() {
double temp;
temp = b * b - 4 * a * c;
if (temp < 0) {
this.i[0] = (-b) / (a * 2) + "+" + Math.sqrt(-temp) / (2 * a) + "i";
this.i[1] = (-b) / (a * 2) + "-" + Math.sqrt(-temp) / (2 * a) + "i";
return false;
} else {
this.d[0] = (-b + Math.sqrt(temp)) / (2 * a);
this.d[1] = (-b - Math.sqrt(temp)) / (2 * a);
return true;
}
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getD(int i) {
return d[i];
}
public static void main(String[] args) {
zuoye2_3 shizi = new zuoye2_3();
shizi.setValue(1, 3, 4);
if (shizi.canGetValue()) {
System.out.println(shizi.getA());
System.out.println(shizi.getB());
System.out.println(shizi.getC());
System.out.println(shizi.getD(0));
System.out.println(shizi.getD(1));
} else {
System.out.println(shizi.i[0]);
System.out.println(shizi.i[1]);
}
}
}
Complex 类
public class Complex {
int x,y;//定义两个属性 实部和虚部
Complex(){//默认构造函数
x=0;
y=0;
}
int getx(){
return x;
}
int gety(){
return y;
}
void showcomp(){//显示复数的方法
if(x==0){
System.out.println(y+"i");
}
else if(y>0)
System.out.println(x+"+"+y+"i");
else if(x==0&&y==0){
System.out.println("0");
}
else
System.out.println(x+""+y+"i");
}
Complex(int i,int j){
x=i;
y=j;
}
static Complex addcomp(Complex c1,Complex c2){
Complex b= new Complex(c1.getx()+c2.getx(),c2.gety()+c1.gety());
return b;
}
static Complex subcomp(Complex c1,Complex c2){
Complex b = new Complex(c1.getx()-c2.getx(),c1.gety()-c2.gety());
return b;
}
static Complex mulitcomp(Complex c1,Complex c2){
Complex b = new Complex(c1.getx()*c2.getx()-c1.gety()*c2.gety(),c1.getx()*c2.gety()+c2.getx()*c1.gety());
return b;
}
public static boolean equalComp(Complex c1,Complex c2){
boolean b;
if(c1.x==c2.x&&c1.y==c2.y)
b=true;
else
b=false;
return b;
}
}
import java.util.*;
public class zuoye2_4{
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
int x=reader.nextInt();
int y=reader.nextInt();
int x2=reader.nextInt();
int y2=reader.nextInt();
Complex c1,c2,c3;
boolean c4;
c1 = new Complex(x,y);
c2 = new Complex(x2,y2);
c3 = new Complex();
System.out.println("计算二个复数的加法");
c3=Complex.addcomp(c1, c2);
c3.showcomp();
System.out.println("计算二个复数的减法");
c3=Complex.subcomp(c1, c2);
c3.showcomp();
System.out.println("计算二个复数的乘法");
c3=Complex.mulitcomp(c1, c2);
c3.showcomp();
System.out.println("判断二个复数是否相等");
c4=Complex.equalComp(c1, c2);
if(c4){
System.out.println("二个复数是相等");
}
else
System.out.println("二个复数不相等");
}
}