实验四 类与对象的方法的权限控制

1【问题描述】定义Student类,包含三个私有属性:name、id、score,根据输出结果,填写程序的空白处。
构造方法中,如果输入的分数不在0~100之间,则对score属性赋值为0。
在对score属性赋值的set方法中,如果给的值不在0~100之间,则给出错误信息,并且不对score进行修改。

【输入形式】
从键盘输入姓名、学号、成绩。

【输出形式】
输出学生的相关信息。

package com;

import java.util.Scanner;
public class Student {
	private String name, id;
	private int score;

	Student(){ }
    Student(String name,String id,int score){
    	this.name=name;
    	this.id=id;
    	this.score=score;
    	if(score<0||score>100) this.score=0;	
    	}
    
    	void setScore(int score){
    	if(score<0||score>100) System.out.println("score input error");
    	} 
    	
    	public String toString() {
//    	这个函数能主动调用 toString 库函数,用toString函数产生的字符串来输出要输出的东西.
			return "name="+this.name+",id="+this.id+",score="+this.score; 		
    	}


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		String n = in.nextLine();
		String s = in.nextLine();
		int sc = in.nextInt();
		Student stu = new Student(n, s, sc);
		System.out.println(stu);
		
		stu.setScore(sc + 50);
		System.out.println(stu);
	}
}

2【问题描述】
输入三角形的三边长,计算面积和周长。
类中有构造方法、关于属性的set、get方法、计算面积和周长的方法。如果输入的边长不能构成三角形,则对象会拒绝接受该值,并输出相应的错误信息。
如果构造方法中提供的三边不能构成三角形,则三边都赋值为0。
根据已有源代码和输出结果,填写代码空白处,使程序完整。

【输入形式】
三角形的三边长度。

【输出形式】
三角形的面积和周长,或者还包含错误信息。

package com;

import java.util.Scanner;

public class Triangle {
	private double a, b, c;

	Triangle(){}
			    Triangle(double x ,double y ,double z){
			    	if((x+y)<=z||(x+z)<=y|| (y+z)<=x) {this.a=this.b=this.c=0;}
			    	else {    	this.a=x;    	this.b=y;    	this.c=z;}
			    	}
			    void setSide(double x,double y ,double z) {  
			    	if((x+y)<=z||(x+z)<=y|| (y+z)<=x) {this.a=this.b=this.c=0;}
			    	else {    	this.a=x;    	this.b=y;    	this.c=z;}}
			    void setA(double x){
			    	if((x+this.b)<=this.c||(x+this.c)<=this.b|| (this.b+this.c)<=x)
			    		System.out.println("a input error");
			    	else
			    		this.a=x;}
			    void setB(double y) {
			    	if((this.a+y)<=this.c||(this.a+this.c)<=y|| (y+this.c)<=this.a)    		System.out.println("b input error");
			    	else
			    	this.b=y;}
			    void setC(double z) {
			    	if((this.a+this.b)<=z||(this.a+z)<=this.b|| (this.b+z)<=this.a)    		System.out.println("c input error");
			    	else
			    	this.c=z;}
			    double getA() {return this.a;}
			    double getB() {return this.b;}
			    double getC() {return this.c;}
			    double getPerimeter() { return this.a+this.b+this.c;}
			    double getArea() {
			        double p=(this.a+this.b+this.c)/2;
			        double sArea=Math.sqrt(p*(p-a)*(p-b)*(p-c));
			        return sArea;
			    }
			    public String toString() {
			    	return "a="+this.a+",b="+this.b+",c="+this.c;
			    }
// 没有上面这个会输出a,b,c 如下commm.Triangle4@74a14482
			    


	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		double x = in.nextDouble();
		double y = in.nextDouble();
		double z = in.nextDouble();
		Triangle t = new Triangle(x, y, z);  
//构造方法通过new运算符调用,需要将x y z变量加进去
		System.out.println("perimeter=" + t.getPerimeter());
		System.out.println("area=" + t.getArea());
		System.out.println(t);
		t.setSide(t.getA() + 3, t.getB() + 2, t.getC() + 1);
		System.out.println(t);
		t.setA(t.getA() + 20);
		System.out.println(t);
		t.setB(t.getB() + 20);
		System.out.println(t);
		t.setC(t.getC() + 20);
		System.out.println(t);
		in.close();
	}
}

注意:

1构造方法与成员方法是相互独立的

2public可以要可以不要

3参数a b c 与 x y z 要分清楚,如this.a = x

构造方法与成员方法:

作用不同: 
构造方法用于创建类的实例并对实例的成员变量进行初始化;成员方法实现对类中成员变量的操作,提供某些功能。

调用方式不同: 
构造方法通过new运算符调用,成员方法通过对象调用。

形式上来看:

构造方法首先没有返回值,再一个就是方法名必须和类名一样

 

总之:构造方法new出对象,然后通过对象调用成员方法

 

进一步总结:

1先在主函数定义构造方法、成员方法,然后具体去实现

2调用成员方法时,要分清是有参还是无参

3构造方法不用加public或者void,直接写相应的无参或者有参

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值