/**
* 定义一个计算类,计算长方体的面积和体积,要求用静态方法完成。
*/
package Liu;
import java.util.Scanner;
public class Cuboid {
//构造方法
Cuboid() {
}
//求面积
private static double GetArea(double x,double y,double z) {
double area;
area = (x*y + y*z + z*x) * 2;
return area;
}
//求体积
private static double GetVolume(double x,double y,double z) {
double volume;
volume = x*y*z;
return volume;
}
//main方法
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
double x,y,z;
x = scanner.nextDouble();
y = scanner.nextDouble();
z = scanner.nextDouble();
//
//静态方法,不创建对象,直接通过类名调用
System.out.println("长方体面积为:" + Cuboid.GetArea(x,y,z));
System.out.println("长方体体积为:" + Cuboid.GetVolume(x,y,z));
}
}
/**输出
*1 2 6
*长方体面积为:40.0
*长方体体积为:12.0
*/
/**
* 定义并实现一个复数类Complex,要求实现复数的加法和减法。
* 类拥有两个成员变量a和b,分别代表复数的实部和虚部。
* 类有默认构造方法(a和b的默认值为0),也有拷贝构造方法(参数为复数类对象的引用)。
* 另外还应该有一个Print方法打印复数,打印格式为a+bi,若实部虚部都为0,则输出0。
* 两个复数的实部、虚部在main方法中由键盘输入。
*/
package Liu;
import java.util.Scanner;
public class complex {
//a为实部,b为虚部
private int a, b;
//默认构造方法
complex() {
this.a = 0;
this.b = 0;
}
//拷贝构造方法
complex(int real, int imag){
this.a = real;
this.b = imag;
}
//复数加法
static complex ComplexAdd(complex x,complex y) {
complex result = new complex();
result.a = x.a + y.a;
result.b = x.b + y.b;
return result;
}
//复数减法
static complex ComplexSub(complex x,complex y) {
complex result = new complex();
result.a = x.a - y.a;
result.b = x.b - y.b;
return result;
}
//复数乘法
static complex ComplexMul(complex x,complex y) {
complex result = new complex();
result.a = x.a*y.a - x.b*y.b;
result.b = x.a*y.b + x.b*y.a;
return result;
}
//复数除法
static complex ComplexDiv(complex x,complex y) {
complex result = new complex();
y.b = 0-y.b;
result = ComplexMul(x, y);
int temp = y.a*y.a + y.b*y.b;
result.a = result.a/temp;
result.b = result.b/temp;
return result;
}
//复数输出
void PrintComplex() {
if(this.a==0&&this.b==0){//实部虚部同时为0
System.out.println("0");
}else if(this.b<0){ //当虚部为负数时
System.out.println(this.a+"-"+(-this.b)+"i");
}else{
System.out.println(this.a+"+"+this.b+"i");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int a,b;
a = scanner.nextInt();
b = scanner.nextInt();
complex x = new complex(a,b);
x.PrintComplex();
int c,d;
c = scanner.nextInt();
d = scanner.nextInt();
complex y = new complex(c,d);
y.PrintComplex();
System.out.print("两个复数相加为:");
complex.ComplexAdd(x, y).PrintComplex();
System.out.print("两个复数相减为:");
complex.ComplexSub(x, y).PrintComplex();
System.out.print("两个复数相乘为:");
complex.ComplexMul(x, y).PrintComplex();
}
}
/**
1 2 3 4
1+2i
3+4i
两个复数相加为:4+6i
两个复数相减为:-2-2i
两个复数相乘为:-5+10i
*/
/**
* 定义一个 point类,该类具有两个构造方法。
* 按顺序实现下属操作:
* 1.定义一个point类的对象A,用参数(x,y)初始化。(x,y从键盘输入)
* 2.定义一个point类的对象B,用A初始化B。
* 3.输出B.GetX()。
* 4.调用fun1(),对象B作为fun1的实参
* 5.执行 B=fun2()。
* 6.输出 B.GetX()。
*/
package Liu;
import java.util.Scanner;
import java.util.concurrent.CancellationException;
public class point {
//两个成员属性
private int x,y;
//第一个构造方法
point(int x,int y){
this.x = x;
this.y = y;
}
//第二个构造方法
point(point B){
this.x = B.x;
this.y = B.y;
}
//获得私有属性x
public int GetX() {
return this.x;
}
//获得私有属性y
public int GetY() {
return this.y;
}
//fun1()
public static void fun1(point p){
System.out.println(p.GetX());
}
//fun2()
public static point fun2()
{
point p =new point(1,2);
return p;
}
//main方法
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
point A = new point(m, n);
point B = new point(A);
System.out.println(B.GetX());
point.fun1(B);
B = point.fun2();
System.out.println(B.GetX());
}
}
/**
输入:
4 5
输出:
4
4
1
*/
/**
* 编写一个Dog类,其中的私有成员包括,name,color,age。
* 请使用get,set方法访问私有变量。
* Dog类的无参构造方法,name=dog1,color=white,age=1。
* 有参构造方法name,color,age从键盘上输入。
* 请在主类中,创建dog1,用无参构造方法,dog2为有参构造方法。
* 输出语句为:System.out.println("name=" + d1.getName() + ",color=" +d1.getcolor()+",age="+d1.getAge());
*/
package Liu;
import java.util.Scanner;
public class Dog {
private String name,color; //声明私有成员
private int age;
public String GetName() { //访问name
return this.name;
}
public String GetColor() { //访问color
return this.color;
}
public int GetAge() { //访问age
return this.age;
}
Dog(){ //无参数构造方法
this.name = "dog1";
this.color = "white";
this.age = 1;
}
Dog(String name,String color,int age){ //有参数构造方法
this.name = name;
this.color = color;
this.age = age;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
Dog dog1 = new Dog(); //创建对象dog1
String name = scanner.nextLine();
String color = scanner.nextLine();
int age = scanner.nextInt();
Dog dog2 = new Dog(name,color,age);//创建对象dog2
//输出
System.out.println("name=" + dog1.GetName() + ",color=" +dog1.GetColor()+",age="+dog1.GetAge());
System.out.println("name=" + dog2.GetName() + ",color=" +dog2.GetColor()+",age="+dog2.GetAge());
}
}
/**
peter
red
6
name=dog1,color=white,age=1
name=peter,color=red,age=6
*/
/**
* 创建一个类,为该类定义三个构造函数,
* 从键盘输入两个int值,三个double值,两个字符串,分别执行下列操作:
* 1、传递两个整数值并找出其中较大的一个值
* 2、传递三个double值并求出其乘积
* 3、传递两个字符串值并检查其是否相同
* 4、在main方法中测试构造函数的调用
*/
package Liu;
import java.util.Scanner;
public class check {
check(int x,int y){
if(x>y) {
System.out.println("两个整数值其中较大的一个值为:" + x);
}else {
System.out.println("两个整数值其中较大的一个值为:" + y);
}
}
check(double a,double b,double c){
double temp;
temp = a*b*c;
System.out.println("三个double值的乘积为:" + temp);
}
check(String str1,String str2){
if(str1.equals(str2)==true) {
System.out.println("true");
}else {
System.out.println("false");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
check ch1 = new check(x, y);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
check ch2 = new check(a, b, c);
String str1 = scanner.next();
String str2 = scanner.next();
check ch3 = new check(str1, str2);
}
}
/**
1 2
两个整数值其中较大的一个值为:2
3 4 5
三个double值的乘积为:60.0
falsj
dskjc
false
*/