将一个数组逆序输出
可以通过循环遍历数组元素,将数组中的元素从后向前依次输出,即可实现将一个数组逆序输出。具体实现可以使用两个指针分别从数组的首尾开始遍历,交换它们所指向的元素的位置,并将指针向中间移动,直到两个指针相遇为止。
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
// 反转数组
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// 输出反转后的数组
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
运行程序后会输出以下结果:
5 4 3 2 1
定义和初始化一个5个元素的数组,利用冒泡排序方法对数组进行排序,并将排序前和排序后的数组元素分别打印出来。
冒泡排序是一种简单的排序算法,其基本思想是通过不断交换相邻元素的位置,将较大(或较小)的元素逐步“冒泡”到数组的末尾。具体实现时,可以使用两层循环,外层循环控制需要比较的轮数,内层循环依次比较相邻元素的大小,并交换它们的位置。
代码示例:
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 3, 1, 4, 2};
// 冒泡排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出排序前的数组
System.out.print("排序前的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
// 输出排序后的数组
System.out.print("排序后的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
运行程序后会输出以下结果:
排序前的数组:5 3 1 4 2
排序后的数组:1 2 3 4 5
编写一个类Rect,Rect类里包含两个函数,一个用来计算矩形面积,一个用来计算矩形周长,再编写测试类Test,Test里面包含主函数,并在主函数里利用Rect申明对象,分别计算对象的面积和周长。
package chap02;
class Rect {
private double length;
private double width;
public Rect(double length,double width) {
this.length=length;
this.width=width;
}
public double perimeter(){
return (length+width)*2;
}
public double area(){
return length*width;
}
}
public class Test1 {
public static void main(String[] args) {
Rect sc=new Rect(3.0,4.0);
System.out.println("矩形的周长为:"+sc.perimeter());
System.out.println("矩形的面积为:"+sc.area());
}
}
运行程序后会输出以下结果:
矩形的面积是:12.0
矩形的周长是:14.0
定义一个矩形类,类里包含长、宽、周长、面积四个属性,计算周长和面积的方法各一个,编写测试类,利用矩形类申明两个对象并进行初始化,分别初始化长和宽,然后计算这两个矩形的面积和周长,通过屏幕打印出来。
public class Rectangle {
private double length; // 矩形的长度
private double width; // 矩形的宽度
private double perimeter; // 矩形的周长
private double area; // 矩形的面积
public Rectangle(double length, double width) { // 构造方法,用于初始化矩形的长度和宽度
this.length = length;
this.width = width;
this.perimeter = 2 * (length + width);
this.area = length * width;
}
public double getPerimeter() { // 计算矩形的周长
return perimeter;
}
public double getArea() { // 计算矩形的面积
return area;
}
}
public class TestRectangle {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(3.0, 4.0); // 声明一个矩形对象,长为3,宽为4
double perimeter1 = rect1.getPerimeter(); // 计算第一个矩形的周长
double area1 = rect1.getArea(); // 计算第一个矩形的面积
System.out.println("第一个矩形的周长是:" + perimeter1);
System.out.println("第一个矩形的面积是:" + area1);
Rectangle rect2 = new Rectangle(5.0, 6.0); // 声明另一个矩形对象,长为5,宽为6
double perimeter2 = rect2.getPerimeter(); // 计算第二个矩形的周长
double area2 = rect2.getArea(); // 计算第二个矩形的面积
System.out.println("第二个矩形的周长是:" + perimeter2);
System.out.println("第二个矩形的面积是:" + area2);
}
}
运行程序后会输出以下结果:
第一个矩形的周长是:14.0
第一个矩形的面积是:12.0
第二个矩形的周长是:22.0
第二个矩形的面积是:30.0
通过定义类和对象的创建、使用,实现输出如下个人信息:“刘阳,男,身高170.0cm。”
要求:
1)定义一个People类,类体内容的变量定义部分定义了1个double类型变量 “sg” (身高)、两个字符串类型变量“xm” (姓名)、 “xb” (性别),和一个方法“rwjj” (人物简介)用来输出个人信息。
2)定义一个主类Test,并在主类中调用缺省构造方法定义一个People类的对象xPeople,要求调用rwjj方法输出以下语句“刘阳,男,身高170.0cm。”
package chap02;
class People {
private double sg;
private String xm;
private String xb;
public People(String xm,String xb,double sg){
this.xm=xm;
this.xb=xb;
this.sg=sg;
}
public void rwjj() {
System.out.println(xm+","+xb+","+"身高"+sg+"cm。");
}
}
public class Test {
public static void main(String[] args) {
People xPeople=new People("刘阳","男",170);
xPeople.rwjj();
}
}
编写程序计算某年某月有几天。其中判别闰年的条件是:能被4整除但不能被100整除的年是闰年,能被400整除的年也是闰年。用switch语句实现。
package chap02;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
System.out.print("请输入月份:");
int month = input.nextInt();
int days;
switch (month) {
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
System.out.println(year + "年" + month + "月有" + days + "天。");
}
}
定义一个Employee类,在该类中定义了三个私有成员变量name、salary和id分别用来记录员工的姓名、薪水和员工编号信息,还定义了一个带参数的构造方法用来初始化员工的姓名、薪水和员工编号信息。
2)定义主类TestPrivateDemo,在主类中调用带参数的构造方法生成一个对象e,并输出该对象的姓名、员工编号和薪水信息。输出信息如下:“员工编号:1,姓名:张三,年薪: 50000.0”。
- 自行编程,掌握通过定义setter方法,正确设置私有变量值的方法。
算法分析:
1)在Emplyee类中定义可以设置name、salary和id变量值的setter方法。
2)在主类TestPrivateDemo中,通过调用三个setter方法重新设置三个成员变量的值,并输出下列信息:“员工编号:2,姓名:李四,年薪:25000.0”。
package chap02;
class Employee {
private String name;
private double salary;
private int id;
public Employee(String name, double salary, int id) {
this.name = name;
this.salary = salary;
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
public class TestPrivateDemo {
public static void main(String[] args) {
Employee e = new Employee("张三", 50000.0, 1);
System.out.println("员工编号:" + e.getId() + ",姓名:" + e.getName() + ",年薪:" + e.getSalary());
e.setId(2);
e.setName("李四");
e.setSalary(25000.0);
System.out.println("员工编号:" + e.getId() + ",姓名:" + e.getName() + ",年薪:" + e.getSalary());
}
}
编程求1+2!+3!+…+10! 的和。
public class FactorialSum {
public static void main(String[] args) {
long sum = 0;
long factorial = 1;
for (int i = 1; i <= 10; i++) {
factorial *= i;
sum += factorial;
}
System.out.println("1+2!+3!+...+10!的和为:" + sum);
}
}
运行程序后,打印出1+2!+3!+…+10!的和。
编写判断输入的年份是否是闰年的程序,闰年的条件是能被4整除,但不能被100整除;能被4整除,又能被400整除。要求用If语句的嵌套实现。
package chap02;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个年份:");
int year = input.nextInt();
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
System.out.println(year + "年是闰年");
} else {
System.out.println(year + "年不是闰年");
}
} else {
System.out.println(year + "年是闰年");
}
} else {
System.out.println(year + "年不是闰年");
}
}
}
编写程序,输出以下图案。
*
***
*****
*******
package chap02;
public class Test {
public static void main(String[] args) {
for(int i=1;i<=4;i++) {
for(int j=1;j<=2*i-1;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
定义一个Employee类,在该类中定义了三个私有成员变量name、salary和id分别用来记录员工的姓名、薪水和员工编号信息,还定义了一个带参数的构造方法用来初始化员工的姓名、薪水和员工编号信息。
2)定义主类TestPrivateDemo,在主类中调用带参数的构造方法生成一个对象e,并输出该对象的姓名、员工编号和薪水信息。输出信息如下:“员工编号:1,姓名:张三,年薪: 50000.0”。
- 自行编程,掌握通过定义setter方法,正确设置私有变量值的方法。
算法分析:
1)在Emplyee类中定义可以设置name、salary和id变量值的setter方法。
2)在主类TestPrivateDemo中,通过调用三个setter方法重新设置三个成员变量的值,并输出下列信息:“员工编号:2,姓名:李四,年薪:25000.0”。
以上是完成数据封装的问题,完成了一个可以设置和输出员工信息的程序,现在要在此基础上进行改进,要求增加能设置和输出公司经理信息的程序,具体要求如下:
1)公司经理的薪水是基本工资与奖金之和。
2)增加一个可以设置和输出职务信息的功能。
3)其余信息项与普通员工一致。
算法分析:
1)定义父类Employee,Employee中中定义可以设置和获取name、salary和id变量值的setter方法和getter方法。
2)定义子类Manager,Manager中覆盖父类的getSalary()方法,使得最后的工资是基本工资和奖金之和。
3)在子类Manager中再定义一个可以设置职务信息的setter方法,同时定义一个可以获取职务信息的getter方法。
4)在主类TestInheritanceDemo中,先创建父类Employee的一个对象,并通过通过调用三个setter方法重新设置三个成员变量的值,并输出下列信息:“员工编号:1,姓名:王笑,年薪:20000.0”。然后创建子类Manager的一个对象,并通过调用四个setter和getter方法,输出下列信息:“员工编号:2,姓名:李东,职务:经理,年薪:50000.0”
package chap02;
class Employee {
private String name;
private double salary;
private int id;
public Employee(String name, double salary, int id) {
this.name = name;
this.salary = salary;
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public int getId() {
return id;
}
}
class Manager extends Employee {
private String position;
public Manager(String name, double salary, int id, String position) {
super(name, salary, id);
this.position = position;
}
public void setPosition(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
public double getSalary() {
return super.getSalary() + 30000.0;
}
}
public class TestInheritanceDemo {
public static void main(String[] args) {
Employee e = new Employee("王笑", 20000.0, 1);
System.out.println("员工编号:" + e.getId() + ",姓名:" + e.getName() + ",年薪:" + e.getSalary());
Manager m = new Manager("李东", 20000.0, 2, "经理");
m.setPosition("经理");
System.out.println("员工编号:" + m.getId() + ",姓名:" + m.getName() + ",职务:" + m.getPosition() + ",年薪:" + m.getSalary());
}
}