java学习之实验三

(1)编写3个基本类: Triangle, Ladder和Circle,分别用来刻画“三角形”、“梯形”和“圆形”类; 1个主类: Compute,负责计算每个形状的面积或周长。
具体要求:
 Triangle 定义3个变量:边长;和1个求周长的方法。
 Ladder 定义3个变量:上底,下底和高;定义1个求面积的方法。

 Circle 定义1个变量:半径;定义2个方法:求面积、求周长。

 3个基本类都要定义相应的构造方法,对变量进行初始化。

解:建立三个类,分别表示三角形,梯形,以及圆形,三角形中定义方法求取周长,以及获取三边长,以此类推分别定义梯形和圆形。

代码如下:

package test3;

class Triangle{
	double  a,b,c;
	public Triangle()	{
		
	}
	public Triangle(double a, double b, double c){
		this.a=a;
		this.b=b;
		this.c=c;
	}
	
	public double C(){
		return a+b+c;
	}
}

class Ladder{
	double up,down,height;
	public Ladder(){
		
	}
	public Ladder(double up, double down, double height){
		this.up=up;
		this.down=down;
		this.height=height;
	}
	public double Area()
	{
		return (up+down)*height/2;
	}
}

class Circle{
	double radius;
	public Circle(){
		
	}
	public Circle(double radius){
		this.radius=radius;
	}
	
	public double Area(){
		return  Math.PI*radius*radius;
	}
	
	public double C(){
		return  Math.PI*radius*2.;
	}
}


public class test3_a{
	public static void main(String[] args) {
		Circle cc=new Circle(3);
		System.out.println(cc.Area());
		System.out.println(cc.C());
		
		Ladder  ld=new Ladder(1,2,3);
		System.out.println(ld.Area());
		
		Triangle tr=new Triangle(1,2,3);
		System.out.println(tr.C());
	}
}


(2)编写一个账户类Account,它包括:一个名为id的int型账号码属性,一个名为balance的double型的账号余额属性,定义一个类型为java.util.Date的属性dateCreated,用于记录账号的创建日期。同时,定义无参的构造函数,一个名为withDraw的方法从账号提取特定数目的金额,一个名为deposit的方法向账号存入特定数目的金额。请编写测试程序,测试各个方法。


package test3;

import java.util.Calendar;
import java.util.Date;

class Account{
	int id;
	double balance;
	Date dateCreated;
	
	public Account(){
		
	}
	public Account(int id, double balance){
		this.id=id;
		this.balance=balance;
		this.dateCreated=(Date) Calendar.getInstance().getTime();
	}
	
	public boolean withDraw(double money){
		if(this.balance>money){
			this.balance-=money;
			return true;
		}
		return false;			
	}
	
	public  void deposit(double money){
		this.balance+=money;
	}
	public double getBalance(){
		return this.balance;
	}
	
}

public class test4_b {	
	public static void main(String[] args) {
		Account ac1=new Account(12,100.);
		boolean result =ac1.withDraw(1000);
		if(result)
			System.out.println("Success");
		else {
			System.out.println("Fail");
			
			ac1.deposit(1000);
			System.out.println(ac1.getBalance());
		}
	}
}<strong>
</strong>

(3)编写一个封装学生的类Student,能够描述学生的“学号”、“ 姓名”、“性别”、“年龄”、“平均成绩”等基本属性,及获取属性、修改属性的方法和打印学生基本信息的print()方法。要求生成两个学生对象,在构造方法中进行初始化,并打印每个学生的基本信息。


package test3;

class Student{
	private  int  id;
	private  String  name;
	private char sex;
	private  int age;
	private double averageScore;
	
	public double getAverageScore(){
		return this.averageScore;
	}
	
	public void setAverageScore(double score){
		this.averageScore=score;
	}
	
	public Student(){
		
	}
	public Student(int id, String  name,  char sex, int age, double score){
		this.id=id;
		this.name=name;
		this.sex=sex;
		this.age=age;
		this.averageScore=score;
	}
	
	public void print()
	{
		System.out.println(id+" "+name+" "+sex+" "+age+" "+averageScore);
	}
}

public class test3_c {

	public static void main(String[] args) {
		Student  st1=new Student(1,"zhang",'f',23,90);
		st1.print();
		st1.setAverageScore(80);
		st1.print();
		
	}
}<strong>
</strong>

附:可在main函数中调用输入函数,此处为方便均未调用。此外类名为方便也并未使用题中规定,读者可自行修正。

Here's a code solution in Python that uses multithreading to calculate the statistical values: ```python import threading import math import sys # Global variables to store the statistical values average = 0 minimum = sys.maxsize maximum = -sys.maxsize median = 0 std_deviation = 0 # A function to calculate the average of the numbers def calculate_average(numbers): global average total = sum(numbers) average = total / len(numbers) # A function to calculate the minimum value def calculate_minimum(numbers): global minimum minimum = min(numbers) # A function to calculate the maximum value def calculate_maximum(numbers): global maximum maximum = max(numbers) # A function to calculate the median value def calculate_median(numbers): global median numbers.sort() n = len(numbers) if n % 2 == 0: median = (numbers[n//2 - 1] + numbers[n//2]) / 2 else: median = numbers[n//2] # A function to calculate the standard deviation def calculate_std_deviation(numbers): global std_deviation n = len(numbers) mean = sum(numbers) / n variance = sum((x - mean)**2 for x in numbers) / (n - 1) std_deviation = math.sqrt(variance) # The main function that creates the worker threads def main(): numbers = [int(x) for x in sys.argv[1:]] t1 = threading.Thread(target=calculate_average, args=(numbers,)) t2 = threading.Thread(target=calculate_minimum, args=(numbers,)) t3 = threading.Thread(target=calculate_maximum, args=(numbers,)) t4 = threading.Thread(target=calculate_median, args=(numbers,)) t5 = threading.Thread(target=calculate_std_deviation, args=(numbers,)) t1.start() t2.start() t3.start() t4.start() t5.start() t1.join() t2.join() t3.join() t4.join() t5.join() print("The average value is", average) print("The minimum value is", minimum) print("The maximum value is", maximum) print("The median value is", median) print("The standard deviation is", std_deviation) if __name__ == '__main__': main() ``` This code creates five worker threads, each responsible for calculating one of the statistical values. The `main()` function parses the command line arguments into a list of numbers, creates the worker threads, starts the threads, and then waits for them to finish. Once all the threads have finished, the main thread outputs the statistical values.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值