本文目录一览:
- 1、Java实验,代码怎么写?
- 2、Java实验,在线等急
- 3、Java程序实验
- 4、Java实验求助
- 5、java实验
Java实验,代码怎么写?
Shape.java接口代码
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圆类代码
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圆柱体类代码
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主类代码
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println("圆的面积为:" + cir1.area());
System.out.println("圆的周长为:" + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println("圆柱体的表面积为:" + cy1.area());
System.out.println("圆柱体的体积为:" + cy1.volume());
}
}
上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。
Java实验,在线等急
运行截图:
ps:如果输入的数不是四位数,则要重新输入。
源代码:
package Ttest;
import java.util.Scanner;
public class Example002 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int x = 0;
boolean flag = true;
System.out.print("请输入四位整数(如3752):");
while(flag) {
x = input.nextInt();
if(x / 1000 == 0 || x / 10000 != 0) {//判断x是否为四位整数
System.out.print("请重新输入:");
}
else {
flag = false;
}
}
int d1 = x % 10;//求模运算
x /= 10;//除法运算
int d2 = x % 10;
x /= 10;//除法运算
int d3 = x % 10;
x /= 10;//除法运算
int d4 = x % 10;
System.out.println("d1=" + d1 + "; d2=" + d2 + "; d3=" + d3 + "; d4=" + d4);
input.close();
}
}
Java程序实验
import java.util.Arrays;
import java.util.Scanner;
public class S {
public static void main(String[] args) {
System.out.println("请输入一个身份证号:");
Scanner scanner = new Scanner(System.in);
String id = scanner.nextLine();
String pattern = "44[0-9]{16}";
if(id.matches(pattern)){
System.out.println("广东");
}else {
System.out.println("非广东或者非身份证号码");
}
System.out.println("请输入字符串:例如AB-C-ABC");
String a1 = scanner.nextLine();
String a2 = a1.replaceAll("A", "First");
String[] a3 = a2.split("-");
System.out.println(Arrays.toString(a3));
System.out.println("请输入第一个字符串:");
String b1 = scanner.nextLine();
System.out.println("请输入第二个字符串:");
String b2 = scanner.nextLine();
if(b1.compareTo(b2) 0){
System.out.println(b2+"在"+b1+"之前");
}else if(b1.compareTo(b2) 0) {
System.out.println(b1+"在"+b2+"之前");
}else {
System.out.println("相同");
}
System.out.println("请输入一个字符串:");
String str = scanner.nextLine();
String[] ch = str.split("");
System.out.println(ch[1]+ch[2]+ch[3]);
}
}
需要设置中文编码
Java实验求助
package cn;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
// 格式:年-月-日 小时:分钟:秒
public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int date = c.get(Calendar.DATE);
if(year==2016month==5date==11)
{
System.out.println("是2016年5月11日");
}
else
{
System.out.println("不是2016年5月11日");
}
//秒
long second=timeSub("2017-05-11 10:10:10","2017-05-12 10:10:10");
//天数
int day=(int) (second/(60*60*24));
System.out.println(second);
System.out.println(day);
}
/**
* 两个日期相减
*
* @param firstTime
* @param secTime
* @return 相减得到的秒数
*/
public static long timeSub(String firstTime, String secTime) {
long first = stringtoDate(firstTime, yyyyMMddHHmmss).getTime();
long second = stringtoDate(secTime, yyyyMMddHHmmss).getTime();
return (second - first) / 1000;
}
/**
* 把符合日期格式的字符串转换为日期类型
*
* @param dateStr
* @return
*/
public static java.util.Date stringtoDate(String dateStr, String format) {
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
formater.setLenient(false);
d = formater.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
d = null;
}
return d;
}
}
java实验
import java.util.Scanner;
public class MyExam1_4 {
public static double getArea(double r) {
final double PI = 3.14; // 定义常量 PI
return PI * r * r; // 计算圆的面积并返回 // -----1-----
}
public static void main(String[] args) {
double radium, area;
System.out.println("请输入半径:");
radium = new Scanner(System.in).nextDouble(); // -----2-----
area = getArea(radium); // 调用 getArea 方法 // -----3-----
System.out.println("圆的半径是" + radium + " ,面积是" + area);
}
}
完整代码了,楼主可直接测