2021级-JAVA05 类和对象（部分）

15 篇文章 31 订阅

函数题

6-1 sdut-oop-1-利用类实现求和（类和对象）

class Test
{
public int sum(double...values){//接受若干个值，values为最后一个接收的值
int result=0;
for(double i:values){
result+=i;
}
return result;
}
}


6-2 sdut-oop-2 是否能构成正方形（类和对象）

class Point {
int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int dist(Point p) {
int tmp = (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
return tmp;
}
}

6-3 sdut-oop-7 计算长方体的体积与质量（类和对象）

class Cuboid
{
double x, y, z; //长、宽、高
double p;  //密度
public Cuboid(double x, double y, double z, double p) {
if(x > 0 && y > 0 && z > 0 && p > 0)
{
this.x = x;
this.y = y;
this.z = z;
this.p = p;
}
else
{
this.x = 0;
this.y = 0;
this.z = 0;
this.p = 0;
}
}

public double length()  //底面周长
{
return 2*(x + y);
}

public double area()  //底面积
{
return x*y;
}

public double volumn()  //体积
{
return x*y*z;
}

public double weight()  //质量
{
return x*y*z*p;
}
}


6-4 sdut-oop-8 小小算术四则运算器（类和对象）


class Calculator {
int x;
int y;

public Calculator(int x, int y) {
this.x = Math.abs(x);
this.y = Math.abs(y);
}
{
return x + y;
}
public int sub()  //求差
{
return x - y;
}
public int mul()  //求积
{
return x*y;
}
public int div()  //求商
{
if(y != 0)
return x/y;
else
return Integer.MAX_VALUE;
}
}

6-5 sdut-oop-3-矩形类的设计

class Rectangle{
double width,height;

public Rectangle(double width, double height) {
super();
this.width = width;
this.height = height;
}

public String getArea() {
double area=this.width*this.height;
return String.format("%.2f", area);
}

public String getPerimeter() {
double perimeter=2*(this.width+this.height);
return String.format("%.2f", perimeter);
}
}

6-6 sdut-oop-4- 织女的红线（类和对象）

class Point{
double x;
double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double dist(Point p){ //求点（x,y)到p点的距离
return Math.sqrt((x - p.x)*(x - p.x)+(y - p.y)*(y - p.y));
}
}
class Cricle{
double r;
public Cricle(double r) {
this.r = r;
}
public double length(){ //求圆的周长
return 2*Math.PI*r;
}
}


6-7 sdut-oop-6-方形矩阵的运算（类和对象）

class Matrix {
int n; // 属性
int[][] matrix = new int[n][n]; // 属性-矩阵

public Matrix(int row, int[][] matrix) { // 构造方法
this.n = row;
this.matrix = matrix;
}

public Matrix add(Matrix other) { // 矩阵相加
int[][] result = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = this.matrix[i][j] + other.matrix[i][j];
}
}
return new Matrix(n, result); //新建一个矩阵对象返回
}

public Matrix sub(Matrix other) { // 矩阵相减
int[][] result = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = this.matrix[i][j] - other.matrix[i][j];
}
}
return new Matrix(n, result); //新建一个矩阵对象返回
}

public Matrix mul(Matrix other) { // 矩阵相乘
int[][] result = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += this.matrix[i][k] * other.matrix[k][j];
}
}
}
return new Matrix(n, result); //新建一个矩阵对象返回
}

public void show() {
for (int i = 0; i < n; i++) // 目标矩阵的行
{
for (int j = 0; j < n; j++) // 目标矩阵的列
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}


6-8 sdut-fun-oop--区域内点的个数


class Rect {
int x1,y1,x2,y2;  //4个属性,分别表示左上角和右上角的x、y坐标

//构造方法
public Rect(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
//判断点（x,y)是否在本矩形对象内部，在窗边的不计
public boolean isInRect(int x,int y) {
if(x > x1 && x < x2 && y > y1 && y < y2)
return true;
return false;
}
}


6-9 sdut-oop-5 分数加减法（类和对象)

class Fs
{
int fz, fm;

public Fs(int fz, int fm)
{
this.fz = fz;
this.fm = fm;
}

{
int newfz = fz * fs.fm + fm * fs.fz;
int newfm = fm * fs.fm;
int gys = gys(newfz, newfm);
return new Fs(newfz/gys, newfm/gys);
}

public Fs sub(Fs fs)
{
int newfz = fz * fs.fm - fm * fs.fz;
int newfm = fm * fs.fm;
int gys = gys(newfz, newfm);
return new Fs(newfz/gys, newfm / gys);
}

public int gys(int a, int b)
{
int n = Math.max(Math.abs(a), Math.abs(b));
int m = Math.min(Math.abs(a), Math.abs(b));
int t;
while(n != 0)
{
t = m % n;
m = n;
n = t;
}
return m;
}
}

6-10 sdut-fun-oop-矩形

/**
* 2D平面中带的点，有x和y坐标，如：(x,y)。
*/
class Point {
int x;
int y;

/**
* C创建一个坐标为(x,y)的点
* @param x x坐标
* @param y y坐标
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}

/*
* 生成的字符串为:(x,y)
*/
@Override
public String toString() {
return "("+x+","+y+")";
}

/**
* 用dx和dy移动点。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
this.x += dx;
this.y += dy;
}

/**
* @param p 另外一个点
* @return 计算这个点和p点之间的距离。
*/
public double distance(Point p) {
return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
}
}

/**
* 二维尺寸，有宽度和高度。
*/
class Dimension {
int width;
int height;

/**
* 创建具有指定宽度和高度的Dimension。
* @param width——Dimension的宽度
* @param height——Dimension的高度
*/
public Dimension(int width, int height) {
this.width = width;
this.height = height;
}

/*
* 生成的字符串为:"width by height"
*/
@Override
public String toString() {
return this.width + " by " + this.height;
}

/**
* R按宽度和高度的比例调整尺寸。
* 虽然标度是双精度的，但结果应该是整数。
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
this.width *= widthScale;
this.height *= heightScale;
}

/**
* @return 计算这个Dimension的面积.
*/
public int area() {
return width*height;
}
}

/**
* 表示一个矩形，在其左上角有一个点Point和一个维度Dimension.
*
*/
class Rectangle {
Point topleft;
Dimension size;

/**
* 创建一个矩形。
* @param topleft 左上角的坐标
* @param size 它的尺寸
*/
public Rectangle(Point topleft, Dimension size) {
this.topleft = topleft;
this.size = size;
}

/*
* 生成的字符串如下: "Rectangle at (x,y):width by height"
*/
public String toString() {
return "Rectangle at (" + topleft.x + "," + topleft.y + "):" + size.width + " by " + size.height;
}

/**
* 将矩形移动一段距离。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
topleft.move(dx, dy);
}

/**
* 调整矩形的宽度和高度
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
size.resize(widthScale, heightScale);
}

/**
* @return 这个矩形的面积。
*/
public double area() {
return size.area();
}

/**
* 计算矩形左上角顶点与矩形r左上角点之间的距离。
* @param r 另一个矩形
* @return 这个矩形和r之间的距离。
*/
public double distance(Rectangle r) {
return this.topleft.distance(r.topleft);
}
}


6-11 模拟题：设计一个BankAccount类

class BankAccount{
int balance;
static int accountNumber=0;
BankAccount(){
accountNumber++;
balance=0;
}
BankAccount(int balance){
accountNumber++;
this.balance=balance;
}
public int getBalance() {
return balance;
}
public int withdraw(int amount) {
if(amount<0||amount>balance) {
System.out.println("Invalid Amount");
return balance;
}
balance=balance-amount;
return balance;
}
public int deposit(int amount){
if(amount<0) {
System.out.println("Invalid Amount");
return balance;
}
balance=balance+amount;
return balance;
}
}


6-12 创建一个直角三角形类实现IShape接口

class RTriangle implements IShape{
private double a;
private double b;
RTriangle(double a, double b){
super();
this.a = a;
this.b = b;
}

@Override
public double getArea() {
return this.a * this.b / 2.0;
}

@Override
public double getPerimeter() {
return this.a + this.b + Math.sqrt(a * a + b * b);
}
}


6-13 矩形

/**
* 2D平面中带的点，有x和y坐标，如：(x,y)。
*/
class Point {
int x;
int y;

/**
* C创建一个坐标为(x,y)的点
* @param x x坐标
* @param y y坐标
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}

/*
* 生成的字符串为:(x,y)
*/
@Override
public String toString() {
return "("+x+","+y+")";
}

/**
* 用dx和dy移动点。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
this.x += dx;
this.y += dy;
}

/**
* @param p 另外一个点
* @return 计算这个点和p点之间的距离。
*/
public double distance(Point p) {
return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
}
}

/**
* 二维尺寸，有宽度和高度。
*/
class Dimension {
int width;
int height;

/**
* 创建具有指定宽度和高度的Dimension。
* @param width——Dimension的宽度
* @param height——Dimension的高度
*/
public Dimension(int width, int height) {
this.width = width;
this.height = height;
}

/*
* 生成的字符串为:"width by height"
*/
@Override
public String toString() {
return this.width + " by " + this.height;
}

/**
* R按宽度和高度的比例调整尺寸。
* 虽然标度是双精度的，但结果应该是整数。
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
this.width *= widthScale;
this.height *= heightScale;
}

/**
* @return 计算这个Dimension的面积.
*/
public int area() {
return width*height;
}
}

/**
* 表示一个矩形，在其左上角有一个点Point和一个维度Dimension.
*
*/
class Rectangle {
Point topleft;
Dimension size;

/**
* 创建一个矩形。
* @param topleft 左上角的坐标
* @param size 它的尺寸
*/
public Rectangle(Point topleft, Dimension size) {
this.topleft = topleft;
this.size = size;
}

/*
* 生成的字符串如下: "Rectangle at (x,y):width by height"
*/
public String toString() {
return "Rectangle at (" + topleft.x + "," + topleft.y + "):" + size.width + " by " + size.height;
}

/**
* 将矩形移动一段距离。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
topleft.move(dx, dy);
}

/**
* 调整矩形的宽度和高度
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
size.resize(widthScale, heightScale);
}

/**
* @return 这个矩形的面积。
*/
public double area() {
return size.area();
}

/**
* 计算矩形左上角顶点与矩形r左上角点之间的距离。
* @param r 另一个矩形
* @return 这个矩形和r之间的距离。
*/
public double distance(Rectangle r) {
return this.topleft.distance(r.topleft);
}
}


6-14 jmu-Java-03面向对象基础-clone方法、标识接口、深拷贝

class Car implements Cloneable
{
private String name;
private CarDriver driver;
private int[] scores;
public Car() {
}
@Override
public String toString() {
return "Car [name=" + name + ", driver=" + driver + ", scores=" + Arrays.toString(scores) + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CarDriver getDriver() {
return driver;
}
public void setDriver(CarDriver driver) {
this.driver = driver;
}
public int[] getScores() {
return scores;
}
public void setScores(int[] scores) {
this.scores = scores;
}
@Override
protected Car clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
Car car = new Car();
CarDriver D = new CarDriver();
if(driver==null)
D=null;
else
D.setName(driver.getName());
car.setDriver(D);
String Nm;
if(name==null)
Nm=null;
else
Nm=name;
car.setName(Nm);
if(scores==null)
car.setScores(null);
else
{
int c[] = Arrays.copyOf(scores, scores.length);
car.setScores(c);
}
return car;
}
}


编程题

7-1 sdut-oop-1 简单的复数运算

import java.util.*;

public class Main {
public static void main(String[] args) {
{
if(e==1)
{
a=a+c;
b=b+d;
}
else  if(e==2)
{
a=a-c;
b=b-d;
}
else if(e==3)
{
int x=a*c-b*d;
int y=c*b+a*d;
a=x;
b=y;
}
else if(c==0&&d==0&&e==0)
{
System.out.println(a+" "+b);
break;
}
}
}
}


import java.util.Scanner;
class Complex {
int real, image;
public Complex(int real, int image) {
this.real = real;
this.image = image;
}
int r = this.real + other.real;
int i = this.image + other.image;
this.real=r;
this.image=i;
}
public void minus(Complex other) {
int r = this.real - other.real;
int i = this.image - other.image;
this.real=r;
this.image=i;
}
public void mul(Complex other) {
int r = this.real * other.real - this.image * other.image;
int i = this.image * other.real + this.real * other.image;
this.real=r;
this.image=i;
}
public String toString() {
return this.real + " " + this.image;
}
}
public class Main {
public static void main(String[] args) {
int a, b, c;
if (a == 0 && b == 0 && c == 0) {
System.out.println(c1);
break;
}

Complex other = new Complex(a, b);

if (c == 1)
else if (c == 2)
c1.minus(other);
else if (c == 3)
c1.mul(other);
}
}
}

7-2 sdut-oop-2 Shift Dot(类和对象）

import java.util.Scanner;

class Point
{
int x, y;

public Point()
{
super();
}
public Point(int x, int y)
{
super();
this.x = x;
this.y = y;
}
public Point move(int x1, int y1)
{
x += x1;
y += y1;
return new Point(x, y);
}
@Override
public String toString()
{
return "("+x+","+y+")";
}
}

public class Main
{
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
Point p = new Point(cin.nextInt(), cin.nextInt());
int n = cin.nextInt();
for(int i = 0; i < n; i ++)
{
p.move(cin.nextInt(), cin.nextInt());
}
System.out.println(p);
}
}
}

7-3 sdut-oop-3 输出人的信息（程序改错）

public class Main {
public static void main(String[] args) {
System.out.println("zhangsan 18");
System.out.println("lisi 20");
System.out.println("wangwu 22");
}
}

7-4 sdut-oop-4-求圆的面积（类与对象）

import java.util.Scanner;
class Circle{
public Circle(){
System.out.println("This is a constructor with no para.");
}
public Circle(int r){
System.out.println("This is a constructor with para.");
}
public void setter(int r){
}
public int getter(){
}
public float getArea(){
return (float)((Math.round(A * 100)) / 100.0);

}
public String toString(){
}
}

public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Circle c1 = new Circle();
System.out.println(c1.toString());
System.out.println(c1.getArea());
Circle c2 = new Circle();
System.out.println(c2.toString());
System.out.println(c2.getArea());
c2.setter(in.nextInt());
System.out.println(c2.toString());
System.out.println(c2.getArea());
Circle c3 = new Circle(in.nextInt());
System.out.println(c3.toString());
System.out.print(+c3.getArea());
}
}



7-5 sdut-oop-9 计算长方形的周长和面积（类和对象）

import java.util.Scanner;
class Rect{
private int length;
private int width;

public int getWidth() {
return width;
}

public int getLength() {
return length;
}

public Rect(int length){
this.length=length;
}
public Rect(int length,int width){
this.length=length;
this.width=width;
}
public int area(){
return length*width;
}
public int area1(){
return length*length;
}
public int c(){
return 2*(length+width);
}
public int c1(){
return 4*length;
}
}
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
String o = s.nextLine();
String [] c=o.split(" ");

int[] a = new int[c.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(c[i]);
}
if(a.length==1)
{
Rect r=new Rect(a[0],a[0]);
if(a[0]<0) System.out.println("0 0 0 0");
else System.out.println(r.getLength()+" "+r.getWidth()+" "+r.c1()+" "+r.area1());
}
else if(a.length==2){
Rect r=new Rect(a[0],a[1]);
if(a[0]<=0||a[1]<=0) System.out.println("0 0 0 0");
else System.out.println(r.getLength()+" "+r.getWidth()+" "+r.c()+" "+r.area());
}
else System.out.println("0 0 0 0");
}

}
}

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext())
{
Rect rect;
String str=sc.nextLine();
String array[]=str.split(" ");
int num=array.length;
if(num==1)
{
int length=Integer.parseInt(array[0]);
rect=new Rect(length,length);
}
else
{
int length=Integer.parseInt(array[0]);
int width=Integer.parseInt(array[1]);
rect=new Rect(length,width);
}
System.out.printf("%d %d %d %d\n",rect.getLength(),rect.getWidth(),rect.circum(),rect.Area());

}
}
}
class Rect{
private int length;
private int width;

public void setLength(int length)
{
if(length<0)
{
this.length=0;
}
else
{
this.length=length;
}
}
public int getLength()
{
return length;
}
public void setWidth(int width)
{
if(width<0)
{
this.width=0;
}
else
{
this.width=width;
}
}
public int getWidth()
{
return width;
}

public Rect(int length)
{
setLength(length);
}
public Rect(int length,int width)
{
if(length<=0||width<=0)
{
length=0;
width=0;
}
this.length=length;
this.width=width;
}
public int Area()
{
return length*width;
}
public int circum()
{
return 2*(length+width);
}

}


7-6 sdut-谁是最强的女汉子


import java.util.Scanner;

class Man {
int beaut;
int streng;

public Man(int beaut, int streng) {
super();
this.beaut = beaut;
this.streng = streng;
}
}

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
Man[] man = new Man[t];
for (int i = 0; i < t; i++) {
int a = input.nextInt();
int b = input.nextInt();
man[i] = new Man(a, b);
}
int min = man[0].beaut;
int sum1 = 0;
long sum = 0;
for (int i = 1; i < t; i++) {
if (man[i].beaut < min)
min = man[i].beaut;
}
for (int i = 0; i < t; i++) {
if (man[i].beaut == min) {
sum1++;
sum += man[i].streng;
}
}
System.out.println(sum1 + " " + sum);
}
}

7-7 sdut-oop-8 分数四则运算（类和对象）

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t -- != 0){
String str=sc.next();
char[] ch=str.toCharArray();
int g=0;//分割加减乘除号；
int f=0;//1代表加号，2代表减号，3代表乘，4代表除号
for(int i=0;i<str.length();i++){
if(ch[i]=='+'){
g=i;
f=1;
break;
}
else if(ch[i]=='-'){
g=i;
f=2;
break;
}
else if(ch[i]=='*'){
g=i;
f=3;
break;
}
else if(ch[i]=='\\'){
g=i;
f=4;
break;
}

}
String s1=str.substring(0, g);
String s2=str.substring(g+1,str.length());
String p[]=s1.split("\\/");//分割\
String q[]=s2.split("\\/");
int a=Integer.parseInt(p[0]);//字符串类型转换成整形
int b=Integer.parseInt(p[1]);
int c=Integer.parseInt(q[0]);
int d=Integer.parseInt(q[1]);
Fs fs1=new Fs(a,b);
Fs fs2=new Fs(c,d);

Fs result=new Fs();//构建Fs类的对象result,为空；对应下面的public Fs(){}

if(f==1){
}

else if(f==2){
result=fs1.sub(fs2);
}
else if(f==3){
result=fs1.multiply(fs2);
}
else if(f==4){
result=fs1.divide(fs2);
}

if((result.fz%result.fm)==0){
System.out.println(result.fz/result.fm);
}
else{
System.out.println(result.fz+"/"+result.fm);
}

}
sc.close();
}

}

class Fs{
int fz;
int fm;

public Fs(int fz,int fm){
this.fz=fz;
this.fm=fm;
}

public Fs(){}

//加法运算
int newFz=fz*fs.fm+fm*fs.fz;
int newFm=fm*fs.fm;
int gys=gys(newFz,newFm);
return new Fs(newFz/gys,newFm/gys);
}

public Fs sub(Fs fs){
//减法运算
int newFz=fz*fs.fm-fm*fs.fz;
int newFm=fm*fs.fm;
int gys=gys(newFz,newFm);
return new Fs(newFz/gys,newFm/gys);
}
public Fs multiply(Fs fs){
//乘法运算
int newFz=fz*fs.fz;
int newFm=fm*fs.fm;
int gys=gys(newFz,newFm);
return new Fs(newFz/gys,newFm/gys);

}
public Fs divide(Fs fs){
//除法运算
int newFz=fz*fs.fm;
int newFm=fm*fs.fz;
int gys=gys(newFz,newFm);
return new Fs(newFz/gys,newFm/gys);

}
public int gys(int a,int b){
//求最大公约数
int m=Math.max(Math.abs(a), Math.abs(b));
int n=Math.min(Math.abs(a), Math.abs(b));
int r;
while(n!=0){
r=m%n;
m=n;
n=r;
}
return m;
}

}

7-8 sdut-分数加减法

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
while( in.hasNext() ){

String str = in.next();
char[]s = str.toCharArray();
int fk = 0;
int fh = 0;
int i;
for( i=0; i<str.length(); i++ ){
if( s[i]=='+' ){
fk = i;
fh = 1;
break;
}
else if( s[i]=='-' ){
fk = i;
fh = 2;
break;
}
}

String s1 = str.substring(0, fk);
String s2 = str.substring(fk+1, str.length());
String []p = s1.split("\\/");
String []q = s2.split("\\/");
int a = Integer.parseInt(p[0]);
int b = Integer.parseInt(p[1]);
int c = Integer.parseInt(q[0]);
int d = Integer.parseInt(q[1]);
Fs fs1 = new Fs(a, b);
Fs fs2 = new Fs(c, d);

Fs result = new Fs();

if( fh==1 )
if( fh==2 )
result = fs1.sub(fs2);
if( (result.fz%result.fm) == 0 )
System.out.println(result.fz/result.fm);
else
System.out.println(result.fz+"/"+result.fm);
}
in.close();
}
}
class Fs {

int fz;
int fm;

public Fs( int fz, int fm ){
this.fz = fz;
this.fm = fm;
}

public Fs(){}

public Fs add( Fs fs ){
int newFz = fz*fs.fm + fm*fs.fz;
int newFm = fm*fs.fm;
int gys = gys(newFz,newFm);
return new Fs(newFz/gys, newFm/gys);
}

public Fs sub( Fs fs ){
int newFz = fz*fs.fm - fm*fs.fz;
int newFm = fm*fs.fm;
int gys = gys(newFz,newFm);
return new Fs(newFz/gys, newFm/gys);
}

public int gys( int a, int b ){

int m = Math.max(Math.abs(a), Math.abs(b));
int n = Math.min(Math.abs(a), Math.abs(b));
int r;
while( n!=0 ){
r = m%n;
m = n;
n = r;
}
return m;
}
}

7-9 sdut-Time类的定义与使用

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
while( in.hasNext() ){
int h = in.nextInt();
int m = in.nextInt();
int s = in.nextInt();
Time ti = new Time();
ti.setTime(h, m, s);
ti.showTime();
}
in.close();
}
}

class Time {
int h, m, s;

public void setTime( int h, int m, int s ){
if( h<0 || h>=24 )
h = 12;
this.h = h;
if( m>=0 && m<=59 )
this.m = m;
if ( s>=0 && s<=59 )
this.s = s;
}

public void showTime(){
DecimalFormat x = new DecimalFormat("00");
System.out.println(x.format(h) + ":" + x.format(m) + ":" + x.format(s));
}
}

7-10 定义商品类，封装成员变量，输出对象


//（1）成员变量：商品编号（String） 、商品名称(String)、商品单价(double)
//（2）成员变量封装，定义为私有属性，并为每个成员变量定义getXXXX，setXXXX方法
//（3）定义构造方法，要求带三个参数，参数值用于给成员变量赋值。
//（4）重写toString（）方法，将对象转换为字符串，格式：商品编号，商品名称，商品单价
//测试类要求：
import java.util.Scanner;
class stuff{
private String number;
private String name;
private double price;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
stuff(String number, String name,double price ) {//带参的构造方法
this.name = name;
this.number = number;
this.price = price;
}
}
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String number = sc.next();//控制输入
String name = sc.next();
double price = sc.nextDouble();
stuff a = new stuff(number, name, price);
System.out.println(a.getNumber()+","+a.getName()+","+a.getPrice());//调用方法
}

}



• 3
点赞
• 29
收藏
觉得还不错? 一键收藏
• 3
评论
11-08 9197

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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