(一)学习总结
1.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?
a构造方法:方法名与类的名称相同并且没有返回值的方法叫做构造方法
b构造方法重载:在同一个类当中方法的名称相同,但是方法中的传递参数的类型不同
c程序不能通过编译,foo类中没有无参构造方法
2.运行下列程序,结果是什么?分析原因,应如何修改。
public class Test {
public static void main(String[] args) {
Foo obj1 = new Foo();
Foo obj2 = new Foo();
System.out.println(obj1 == obj2);
}
}
class Foo{
int value = 100;
}
a编译不能通过,类的对象没有进行实例化,必须用new对对象进行实例化
3运行下列程序,结果是什么?说明原因。
public class Test {
public static void main(String[] args) {
Foo obj1 = new Foo();
Foo obj2 = new Foo();
System.out.println(obj1 == obj2);
}
}
class Foo{
int value = 100;
}
a.运行结果是:false
b:原因:每次用new实例化对象都会开辟一个新地址,“==”实际上比较的是内存地址
4.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明。
a:面向对象用关键字private关键字对对象的各种属性进行封装。
b:举例:private int data;这个语句就是对日期属性的封装,如果想要调用属性必须对类设置set,get方法。
5.阅读下面程序,分析是否能编译通过?如果不能,说明原因。
(1)
class A{
private int secret = 5;
}
public class Test{
public static void main(String args[]){
A a = new A();
System.out.println(a.secret++);
}
}
a.编译不能通过,secret是进行封装过的属性不能直接调用,必须用get方法进行获得属性
(2)
public class Test{
int x = 50;
static int y = 200;
public static void method(){
System.out.println(x+y);
}
public static void main(String args[]){
Test.method();
}
}
a:static是静态访问常量,x没有用static进行修饰不能在别的类中直接访问
6.使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。下面给出测试类代码和Book类的部分代码,将代码补充完整。
class Book{
int bookId;
String bookName;
double price;
// 声明静态变量
public static n;
//定义静态代码块对静态变量初始化
static {
n=0;
}
//构造方法
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//定义方法求图书总册数
public int totalBook(){
return n;
}
//重写toString方法
public string toString(){
return "编号"+bookld+“书名”+bookName+"价格"+"price "+"图书总数目为"+n;
}
}
public class Test{
public static void main(String args[]){
Book[] books = {new Book("c语言程序设计",29.3),
new Book("数据库原理",30),
new Book("Java学习笔记",68)};
System.out.println("图书总数为:"+ Book.totalBook());
for(Book book:books){
System.out.println(book.toString());
}
}
}
7.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。
单例模式只有一个实例
1、单例类只能有一个实例。
2、单例类必须自己创建自己的唯一实例。
3、单例类必须给所有其他对象提供这一实例。
public Class Sun{
private Class eg = new Class;
public Sun(){}
public Sun getEg(){
return eg;
}
}
8.理解Java参数传递机制,阅读下面的程序,运行结果是什么?说明理由。
public class Test {
String str = new String("你好 ");
char[] ch = { 'w','o','l','l','d' };
public static void main(String args[]) {
Test test = new Test();
test.change(test.str, test.ch);
System.out.print(test.str);
System.out.print(test.ch);
}
public void change(String str, char ch[]) {
str = "hello";
ch[0] = 'W';
}
}
a.运行结果:你好Wolld
(二)实验总结
用面向对象思想完成评分系统:
package 评分改进;
import java.util.Scanner;
//评分类
class PingFenLei {
private int pws;
XuanShou xs = null;
private float[] pwdf = null;
public PingFenLei() {
} // 定义构造方法
public PingFenLei(int pws, XuanShou xs) { // 含参构造方法
this.setPws(pws);
this.setXs(xs);
}
public void setPws(int p) { // 设置评委数
pws = p;
}
public int getPws() { // 获得评委数
return pws;
}
public float[] getPwdf() { // 获得评委打分分数
return this.pwdf;
}
public void setPwdf() { // 设置评委打分分数
System.out.println("请为" + this.xs.getNumber() + "号选手打分:");
Scanner input = new Scanner(System.in);
// System.out.println(pws);
pwdf = new float[this.pws];
// System.out.println(pwdf.length);
for (int i = 0; i < this.pws; i++) {
float a = input.nextFloat();
this.pwdf[i] = a;
}
}
public XuanShou getXs() {
return this.xs;
}
public void setXs(XuanShou xs) {
this.xs = xs;
}
public float Max() {
int i = 0;
float max;
float p[] = null;
p = getPwdf();
max = p[i];
for (; i < getPws(); i++) {
if (max < pwdf[i]) {
max = pwdf[i];
}
}
System.out.println("去掉最高分" + max);
return max;
}
public float Min() {
int i = 0;
float min;
float p[] = null;
p = getPwdf();
min = p[i];
for (; i < getPws(); i++) {
if (min > pwdf[i]) {
min = pwdf[i];
}
}
System.out.println("去掉最低分" + min);
return min;
}
public float ave() {
float sum = 0;
float ave = 0;
for (int i = 0; i < this.pwdf.length; i++) {
sum += pwdf[i];
}
sum = sum - Max() - Min();
ave = sum / (this.pws - 2);
return ave;
}
}
package 评分改进;
import java.util.Arrays;
import java.util.Scanner;
public class Text {
public static void main(String args[]) {
int xss, pws, i;
String bianhao, name;
Scanner in = new Scanner(System.in);
System.out.println("输入选手数");
xss = in.nextInt();
System.out.println("输入评委数");
pws = in.nextInt();
XuanShou[] xs = new XuanShou[xss];
for (i = 0; i < xss; i++) {
System.out.println("输入选手编号:");
bianhao = in.next();
System.out.println("输入选手姓名:");
name = in.next();
xs[i] = new XuanShou(bianhao, name);
PingFenLei per = new PingFenLei(pws, xs[i]);
per.setPwdf();
xs[i].setScore(per.ave());
System.out.println(bianhao + "号选手最后得分" + xs[i].getScore());
}
System.out.println("排行榜");
Arrays.sort(xs);
for (i = 0; i < xss; i++) {
System.out.println(xs[i]);
}
}
}
package 评分改进;
//选手类
class XuanShou implements Comparable<XuanShou> {
// public static float[] pwdf;
private String number;
private String name;
private float score;
public XuanShou() {
}// 定义无参构造
public XuanShou(String bianhao, String name) {
this.setNumber(bianhao);
this.setName(name);
}; // 定义有参构造方法
public XuanShou(float a) {
this.score = a;
}
public void setName(String n2) { // 设置选手姓名
name = n2;
}
public String getName() { // 取得选手姓名
return name;
}
public void setNumber(String num) { // 设置选手编号
number = num;
}
public String getNumber() { // 取得选手编号
return number;
}
public void setScore(float s) { // 设置选手最终分数
score = s;
}
public float getScore() { // 取得选手最终分数
return score;
}
@Override
public int compareTo(XuanShou o) {// 排序方法
if (this.score > o.score) {
return -1;
} else if (this.score < o.score) {
return 1;
} else {
return 0;
}
}
public String toString() {
return "编号:" + this.number + "姓名:" + this.name + "得分:" + this.score;
}
}
2.Email验证
package 判断邮箱;
import java.util.Scanner;
public class YouXiang {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x;
String c;
String s1="@";
String s2=".";
String end1="com",end2="cn",end3="gov",end4="gov",end5="edu",end6="org";
int a=0,b=0,d=0,e=0,f=0,a1;
Scanner in = new Scanner(System.in);
do {
if(f==0)
System.out.println("请输入您的邮箱地址:");
if(f!=0)
System.out.println("您输入的邮箱地址有误,请您重新输入您的邮箱地址:");
c = in.next();
System.out.println("您输入的邮箱地址为:"+c);
//判断是否包含@和.
if(c.contains(s1)) {
if(c.contains(s2)) {
a=1;
}
}
//判断是否@在.前面
if(c.indexOf(s1)<c.indexOf(s2)) {
b=1;
}
//判断@是不是第一位开头
if(c.indexOf(s1)!=0) {
d=1;
}
if(c.endsWith(end1)||c.endsWith(end2)||c.endsWith(end3)||c.endsWith(end4)||c.endsWith(end5)||c.endsWith(end6)) {
e=1;
}
f++;
}while(a!=1||b!=1||d!=1||e!=1);
System.out.println("您输入的邮箱格式正确");
}
}
3.查找子串
package 字串数;
import java.util.Scanner;
public class ZiChanShu {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i, j, n = 0, k;
String c1;
String c2;
Scanner input = new Scanner(System.in);
System.out.println("请输入字符串:");
c1 = input.next();
System.out.println("请输入要查找的字符串:");
c2 = input.next();
char c3[] = c1.toCharArray();
char c4[] = c2.toCharArray();
// System.out.println("c3.length:" + c3.length);
// System.out.println("c4.length:" + c4.length);
k = c4.length - 1;
// System.out.println("k:" + k);
if (k == 0) {
for(i=0,j=0;i<c3.length;i++) {
if(c3[i]==c4[0]) {
n++;
}
}
} else {
for (i = 0, j = 0; i < c3.length; i++) {
if (c3[i] == c4[j]) {
// System.out.println("i:" + i + " j:" + j);
if (j == k) {
n++;
j = 0;
// System.out.println("j:" + j + " n:" + n);
}
}
else {
j = 0;
}
if (c3[i] == c4[j]&&j!=k)
j++;
else {
j=0;
}
}
}
System.out.println("字串的数量是:" + n);
}
}
- 遇到的问题: 1:输入任何字串,最后显示的字串数量总是为零 解决: 开始误认为j==c4.length时应当
- n++,实则是j==c4.length-1时n++ 2:如果字串的数量是1个的话,字串数量显示0 解决:
- 增加一个if分支,判断如果字串只有一个的话则改变方法;