15 changed files with 394 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,31 @@ |
|||
package Homework; |
|||
|
|||
public class DataCleaner { |
|||
public static void main(String[] args) { |
|||
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; |
|||
|
|||
int validSum = 0; // 有效数据总和
|
|||
int validCount = 0; // 有效数据个数
|
|||
|
|||
// 请在此处编写你的流程控制代码
|
|||
for (int i=0;i<sensorData.length;i++){ |
|||
if(sensorData[i]>=1&&sensorData[i]<=100){ |
|||
validSum+=sensorData[i]; |
|||
validCount++; |
|||
}else if((sensorData[i]>100||sensorData[i]<=0)&&sensorData[i]!=999){ |
|||
System.out.println("警告:发现越界数据 [数值],已跳过"); |
|||
continue; |
|||
}else if(sensorData[i]==999){ |
|||
System.out.println("致命错误:传感器掉线,终止处理"); |
|||
break; |
|||
} |
|||
} |
|||
if (validCount>0){ |
|||
double average=0; |
|||
average=(double)validSum/validCount; |
|||
System.out.println("有效数据平均值为:"+average); |
|||
}else{ |
|||
System.out.println("无有效的数据"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,110 @@ |
|||
import java.util.Scanner; |
|||
|
|||
/** |
|||
* 温度转换器程序 |
|||
* 支持摄氏度 (C) 与华氏度 (F) 之间的双向转换 |
|||
* 支持控制台交互输入和命令行参数两种模式 |
|||
*/ |
|||
public class TemperatureConverter { |
|||
|
|||
/** |
|||
* 将摄氏度转换为华氏度 |
|||
* @param c 摄氏温度值(浮点型) |
|||
* @return 对应的华氏温度值(浮点型) |
|||
*/ |
|||
public static double celsiusToFahrenheit(double c) { |
|||
return c * 9.0 / 5.0 + 32.0; |
|||
} |
|||
|
|||
/** |
|||
* 将华氏度转换为摄氏度 |
|||
* @param f 华氏温度值(浮点型) |
|||
* @return 对应的摄氏温度值(浮点型) |
|||
*/ |
|||
public static double fahrenheitToCelsius(double f) { |
|||
return (f - 32.0) * 5.0 / 9.0; |
|||
} |
|||
|
|||
/** |
|||
* 处理温度转换核心逻辑 |
|||
* @param value 温度数值 |
|||
* @param unit 温度单位(C/F,不区分大小写) |
|||
*/ |
|||
public static void convert(double value, String unit) { |
|||
String upperUnit = unit.toUpperCase(); |
|||
if (upperUnit.startsWith("C")) { |
|||
double f = celsiusToFahrenheit(value); |
|||
System.out.printf("%.2f °C = %.2f °F%n", value, f); |
|||
} else if (upperUnit.startsWith("F")) { |
|||
double c = fahrenheitToCelsius(value); |
|||
System.out.printf("%.2f °F = %.2f °C%n", value, c); |
|||
} else { |
|||
System.out.println("未知单位,请使用 C 或 F。"); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 控制台交互模式:提示用户输入并处理转换 |
|||
*/ |
|||
public static void interactiveMode() { |
|||
Scanner scanner = new Scanner(System.in); |
|||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|||
String input = scanner.nextLine().trim(); |
|||
scanner.close(); |
|||
|
|||
if (input.isEmpty()) { |
|||
System.out.println("输入为空,程序退出。"); |
|||
return; |
|||
} |
|||
|
|||
try { |
|||
String[] parts = input.split(" "); |
|||
double value = Double.parseDouble(parts[0]); |
|||
String unit = parts.length > 1 ? parts[1] : "C"; |
|||
convert(value, unit); |
|||
} catch (Exception e) { |
|||
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 主方法:支持命令行参数和交互两种模式 |
|||
* @param args 命令行参数,格式为 [温度数值] [单位],例如 36.6 C |
|||
*/ |
|||
public static void main(String[] args) { |
|||
// 命令行参数模式 |
|||
if (args.length >= 1) { |
|||
try { |
|||
double value = Double.parseDouble(args[0]); |
|||
String unit = args.length >= 2 ? args[1] : "C"; |
|||
convert(value, unit); |
|||
} catch (Exception e) { |
|||
System.out.println("命令行参数解析失败,请按格式输入:java TemperatureConverter 36.6 C"); |
|||
} |
|||
} else { |
|||
// 交互模式 |
|||
interactiveMode(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 批量转换:从文件读取多行温度数据并输出结果 |
|||
* @param filePath 文件路径,每行格式为「数值 单位」,例如 36.6 C |
|||
*/ |
|||
public static void batchConvertFromFile(String filePath) { |
|||
try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(filePath))) { |
|||
String line; |
|||
while ((line = br.readLine()) != null) { |
|||
String trimLine = line.trim(); |
|||
if (trimLine.isEmpty()) continue; |
|||
String[] parts = trimLine.split(" "); |
|||
double value = Double.parseDouble(parts[0]); |
|||
String unit = parts.length > 1 ? parts[1] : "C"; |
|||
System.out.print("处理行:" + line + " → "); |
|||
convert(value, unit); |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("读取文件失败:" + e.getMessage()); |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,31 @@ |
|||
package Homework; |
|||
|
|||
public class DataCleaner { |
|||
public static void main(String[] args) { |
|||
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; |
|||
|
|||
int validSum = 0; // 有效数据总和
|
|||
int validCount = 0; // 有效数据个数
|
|||
|
|||
// 请在此处编写你的流程控制代码
|
|||
for (int i=0;i<sensorData.length;i++){ |
|||
if(sensorData[i]>=1&&sensorData[i]<=100){ |
|||
validSum+=sensorData[i]; |
|||
validCount++; |
|||
}else if((sensorData[i]>100||sensorData[i]<=0)&&sensorData[i]!=999){ |
|||
System.out.println("警告:发现越界数据 [数值],已跳过"); |
|||
continue; |
|||
}else if(sensorData[i]==999){ |
|||
System.out.println("致命错误:传感器掉线,终止处理"); |
|||
break; |
|||
} |
|||
} |
|||
if (validCount>0){ |
|||
double average=0; |
|||
average=(double)validSum/validCount; |
|||
System.out.println("有效数据平均值为:"+average); |
|||
}else{ |
|||
System.out.println("无有效的数据"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
|
|||
public class BankAccount { |
|||
private String accountNumber; |
|||
private String ownerName; |
|||
private double balance; |
|||
public BankAccount(String accountNumber, String ownerName, double balance){ |
|||
this.accountNumber = accountNumber; |
|||
this.ownerName = ownerName; |
|||
this.balance=0.0; |
|||
} |
|||
public void deposit(double amount){ |
|||
if ( amount >0.0){ |
|||
this.balance+=amount; |
|||
System.out.printf("存款成功,余额为:%f\n",this.balance); |
|||
} else{ |
|||
System.out.println("存款失败,金额必须大于0.0\n"); |
|||
} |
|||
} |
|||
public void withdraw (double amount){ |
|||
if (amount>0.0 &&amount<this.balance){ |
|||
this.balance-=amount; |
|||
System.out.printf("取款成功,余额为:%f\n",this.balance); |
|||
} else{ |
|||
System.out.println("取款失败,金额必须大于0.0且小于余额\n"); |
|||
} |
|||
} |
|||
public double getbalance(){ |
|||
return balance; |
|||
} |
|||
public String getAccountNumber(){ |
|||
return accountNumber; |
|||
} |
|||
public String getOwnerName(){ |
|||
return ownerName; |
|||
} |
|||
public void setAccountNumber(String accountNumber){ |
|||
this.accountNumber = accountNumber; |
|||
} |
|||
|
|||
|
|||
|
|||
} |
|||
|
|||
@ -0,0 +1,49 @@ |
|||
package Homework; |
|||
|
|||
public class Employee { |
|||
private String id; |
|||
private String name; |
|||
private String department; |
|||
private double salary; |
|||
public Employee(String id, String name, String department){ |
|||
this.id = id; |
|||
this.name = name; |
|||
this.department = department; |
|||
} |
|||
public void setSalary(double salary){ |
|||
if (salary>=2000){ |
|||
this.salary = salary; |
|||
} |
|||
} |
|||
public double getSalary(){ |
|||
return salary; |
|||
} |
|||
public void setDepartment(String department){ |
|||
this.department = department; |
|||
} |
|||
public String getDepartment(){ |
|||
return department; |
|||
} |
|||
public void setName(String name){ |
|||
this.name = name; |
|||
} |
|||
public String getName(){ |
|||
return name; |
|||
} |
|||
public void setId(String id){ |
|||
this.id = id; |
|||
} |
|||
public String getId(){ |
|||
return id; |
|||
} |
|||
public void raiseSalary(double percent){ |
|||
double newSalary=this.salary*(1+percent/100); |
|||
if (newSalary>=2000){ |
|||
this.salary = newSalary; |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
@ -0,0 +1,19 @@ |
|||
package Homework; |
|||
|
|||
public class EmployeeTest { |
|||
|
|||
public static void main(String[] args) { |
|||
Employee xiaoming = new Employee("1", "xiaoming", "技术部"); |
|||
xiaoming.setSalary(3000); |
|||
System.out.println(xiaoming.getId() + " " + xiaoming.getName() + " " + xiaoming.getDepartment() + " " + xiaoming.getSalary()); |
|||
xiaoming.raiseSalary(10); |
|||
System.out.println(xiaoming.getId() + " " + xiaoming.getName() + " " + xiaoming.getDepartment() + " " + xiaoming.getSalary()); |
|||
|
|||
Employee xiaohong = new Employee("2", "xiaohong", "销售部"); |
|||
xiaohong.setSalary(2000); |
|||
System.out.println(xiaohong.getId() + " " + xiaohong.getName() + " " + xiaohong.getDepartment() + " " + xiaohong.getSalary()); |
|||
xiaohong.raiseSalary(15); |
|||
System.out.println(xiaohong.getId() + " " + xiaohong.getName() + " " + xiaohong.getDepartment() + " " + xiaohong.getSalary()); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,39 @@ |
|||
package learn; |
|||
|
|||
public abstract class Shape { |
|||
public abstract double getArea(); |
|||
} |
|||
class Circle extends Shape{ |
|||
private double radius; |
|||
public Circle(double radius){ |
|||
this.radius=radius; |
|||
} |
|||
@Override |
|||
public double getArea(){ |
|||
return Math.PI*radius*radius; |
|||
} |
|||
} |
|||
class Rectangle extends Shape{ |
|||
private double length; |
|||
private double width; |
|||
public Rectangle(double length,double width){ |
|||
this.length=length; |
|||
this.width=width; |
|||
} |
|||
@Override |
|||
public double getArea(){ |
|||
return length*width; |
|||
} |
|||
} |
|||
class Triangle extends Shape{ |
|||
private double base; |
|||
private double height; |
|||
public Triangle(double base,double height){ |
|||
this.base=base; |
|||
this.height=height; |
|||
} |
|||
@Override |
|||
public double getArea(){ |
|||
return base*height/2; |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
package learn; |
|||
|
|||
public class ShapeUtil { |
|||
public static void printArea(Shape shape){ |
|||
System.out.println(shape.getArea()); |
|||
} |
|||
public static void main(String[]args){ |
|||
Shape circle=new Circle(5); |
|||
printArea(circle); |
|||
} |
|||
} |
|||
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
@ -0,0 +1,34 @@ |
|||
package Homework; |
|||
|
|||
abstract class Vehicle { |
|||
public abstract void run(); |
|||
} |
|||
class Car extends Vehicle{ |
|||
@Override |
|||
public void run(){ |
|||
System.out.println("car is running"); |
|||
} |
|||
} |
|||
class Bike extends Vehicle{ |
|||
@Override |
|||
public void run(){ |
|||
System.out.println("bike is running"); |
|||
} |
|||
} |
|||
class Truck extends Vehicle{ |
|||
@Override |
|||
public void run(){ |
|||
System.out.println("truck is running"); |
|||
} |
|||
} |
|||
class VehicleTest{ |
|||
public static void main(String[]args){ |
|||
Vehicle[] vehicles=new Vehicle[3]; |
|||
vehicles[0]=new Car(); |
|||
vehicles[1]=new Bike(); |
|||
vehicles[2]=new Truck(); |
|||
for(Vehicle i :vehicles){ |
|||
i.run(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package Homework; |
|||
|
|||
public abstract class shape { |
|||
public abstract void draw(); |
|||
} |
|||
class Circle extends shape{ |
|||
@Override |
|||
public void draw(){ |
|||
System.out.println("绘制一个圆形"); |
|||
} |
|||
|
|||
} |
|||
class Rectangle extends shape{ |
|||
@Override |
|||
public void draw(){ |
|||
System.out.println("绘制一个矩形"); |
|||
} |
|||
} |
|||
class Main{ |
|||
public static void drawShape(shape s){ |
|||
s.draw(); |
|||
} |
|||
public static void main(String[]args){ |
|||
drawShape(new Circle()); |
|||
drawShape(new Rectangle()); |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 54 KiB |
Loading…
Reference in new issue