1. 程式人生 > >Java陣列,構造方法,this關鍵字例題全解--經典問題

Java陣列,構造方法,this關鍵字例題全解--經典問題

1.int[][]  arr = {{5, 7, 9},{12, 14, 16, 18},{23, 25, 36, 47},{22, 54, 65, 15},{22, 34}}; 求該陣列元素之和
class Demo6{
   public static void main(String[] args){
   int[][]  arr = {{5,7,9},{12,14,16,18},{23,25,36,47},{22,54,65,15},{22,34}};
     int sum=0;
    System.out.print("二維陣列和sum=");
for (int i=0;i<arr.length ;i++ )
     {
for (int j=0; j<arr[i].length ;j++ )
{
 sum+=arr[i][j];
}
     }
System.out.println(sum+";");
    }
  }




2.使用二維陣列儲存班上五個學生三門功課的考試成績,要求輸出每一個學生的總分、平均分、最高分、最低分。


要求:
①學生個數動態獲取。
②學生考試的門數動態獲取。
③每個學生各門功課考試的成績動態獲取。
【動態獲取是指由鍵盤輸入】


import java.util.Scanner;
class Demo7{
   public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
   Scanner input = new Scanner(System.in);
   System.out.print("請錄入班級學生的個數:");
int stuNums = input.nextInt();
int[][] scores = new int[stuNums][];
for (int i = 0; i < scores.length; i++) {
System.out.print("\n請錄入第【" + (char)(i +65) + "】個學生考試的功課門數:");
int courseNum = input.nextInt();
scores[i] = new int[courseNum];
int max = 0;
int min = 0;
double totalScore = 0;
double avgScore = 0;
for (int j = 0; j < scores[i].length; j++) {
System.out.print("請錄入該學生第【" +  (char)(j +65) + "】功課的成績:");
scores[i][j] = input.nextInt();
// b)求最高分
if (scores[i][max] < scores[i][j]) {
max = j;
}
// c)最低分
if (scores[i][min] > scores[i][j]) {
min = j;
}
// d)總成績
totalScore += scores[i][j];
}
avgScore = totalScore / scores[i].length;
System.out.println("該學生成績資訊如下:總分--》" + totalScore + ",平均分--》"
+ avgScore + ",最高分---》" + scores[i][max] + ",最低分---》"
+ scores[i][min]);
}
   }
}




3.程式設計列印一個二維陣列中所有元素的和,並列印最大值,最小值(以及它們所在的行號和列號)
void printResult(int a[][]){......}
輸出結果格式:
二維陣列中所有元素的和是:123
最大值是:15,行號:3,列號:1
最小值是:1,行號:2,列號:4


class Demo7 
{
public static void main(String[] args){
int[][] arr = {{2,3,4},{1,2},{5,6,7}};
printResult(arr);
printMax(arr);
}
public static void printResult(int arr[][]){
int sum = 0;
int max = 0;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
sum +=arr[i][j];
}
}
System.out.println("二維陣列中所有元素的和是"+sum);
}
public static void printMax(int arr[][]){
int max = arr[0][0];//儲存最大值
int x = 0;//行號
int y = 0;//列號
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
if(arr[i][j] > max){
max = arr[i][j];
x = i;
y = j;
}
}
}
System.out.println("最大值是:"+max+",行號"+x+",列號"+y);
}
}






利用面向物件的思想寫下面的程式


1.小美在朝陽公園溜旺財【注:旺財是狗】


分析:
Dog:
特性:名稱
Park:
特性:名稱
Person
特性:姓名
行為:溜(Park p,Dog d)


class Park{
String name;
}
class Dog{
String name;
}
public class Demo7{
String name;
void slip(Park p, Dog d){
System.out.println(name + "正在【"+p.name+"】溜【"+d.name+"】");
}
public static void main(String[] args){
Park p = new Park();
p.name = "朝陽公園";
Dog d = new Dog();
d.name = "旺財";
Demo7 pe = new Demo7();
pe.name = "小美";
pe.slip(p, d);
}
}


2.小明穿著白色的特步運動鞋在奧林匹克公園跑步


/*分析:
Park:
特性:名字
Shoes:
特性:顏色,品牌;分類
Person:
特性:姓名, 鞋
行為:
跑(在哪跑)*/


class Park{

String name;
}


class Shoes{
String color;
String brand;
String classify;
}


public class Person{

String name;
// 人有一雙鞋
Shoes s;

void run(Park p){
System.out.println(name + "穿著【"+s.color+"】的【"+s.brand+"】"+s.classify+"在【"+p.name+"】跑步");
}

public static void main(String[] args){
Park p = new Park();
p.name = "奧林匹克公園";

Shoes s = new Shoes();
s.color = "白色";
s.brand = "特步";
s.classify = "運動鞋";

Person pe = new Person();
pe.name = "小明";
pe.s = s;
pe.run(p);
}

}








3.趙老師在講臺上講課,小剛認真的聽課做筆記

/*
 * 
  分析:
Teacher:
特性:姓名
行為:
講課
Student:
特性:姓名
行為:
聽課
做筆記
*/


class Teacher{

String name;

void teach(){

System.out.println(name + "正在講臺上講課");
}
}


class Student{
String name;

void listen(){

System.out.print(name + "正在認真的聽課");
}

void write(){

System.out.print("記筆記");
}
}


public class Demo {

public static void main(String[] args){
Teacher t = new Teacher();
t.name = "趙老師";
t.teach();

Student stu = new Student();
stu.name = "小剛";
stu.listen();
stu.write();
}
}








4.張阿姨和李阿姨在物美超市買紅富士


/*
  分析:
Person:
特性:姓名
行為:
買(位置,東西)
SuperMarket:
特性:姓名


Apple
特性:品牌
*/


class SuperMarket{

String name;
}


class Apple{
String brand;
}


public class Person{
String name;

void buy(Person p,SuperMarket s, Apple a){

System.out.println(name + "和【"+p.name+"】在【"+s.name+"】買【"+a.brand+"】");
}

public static void main(String[] args){
SuperMarket s = new SuperMarket();
s.name = "物美";

Apple a = new Apple();
a.brand = "紅富士";

Person p1 = new Person();
p1.name = "趙阿姨";

Person p2 = new Person();
p2.name = "張阿姨";
p2.buy(p1, s, a);

}
}






構造方法和this關鍵字的使用


1.定義一“圓”(Circle)類,圓心為“點”Point類,構造一圓,求圓的周長和麵積,並判斷某點與圓的關係


分析:
點 - (x, y)
圓 - 半徑和圓心
人 
計算


方式一:
/*
點類
*/
class Point
{
//特徵
double x;
double y;


Point(double x, double y){
this.x = x;
this.y = y;
}
}


/*
圓類
*/
class Circle
{
// 特徵
Point p;  // 圓心點
double r;


Circle(Point p, double r){
this.p = p;
this.r = r;
}
}
class Person
{


// 名字
String name;
// 行為 


// 1. 圓的面積
double getCircleArea(Circle c){

return Math.PI * Math.pow(c.r , 2);
}


// 2.圓的周長
double getCircleLength(Circle c){
return 2*Math.PI*c.r;
}


// 3.點和圓的關係  a:圓內   b:圓上   c:圓外
String relation(Point p, Circle c){
//  d = (x1-x2)^2 + (y1-y2)^2  再開根號    d > r 圓外   d==r 圓上   d < r 圓內


// 求給出的點和圓心之間的距離


// 橫軸之間的距離差的平方
double x = Math.pow( p.x - c.p.x  , 2);
// 縱軸之間的距離差的平方
double y = Math.pow( p.y - c.p.y  , 2);
// 球 x + y 開平方
double d = Math.sqrt( x + y);


if(d > c.r){
return "圓外";
}else if(d == c.r){
return "圓上";
}else {
return "圓內";
}

}


public static void main(String[] args) 
{
System.out.println("Hello World!");


// 例項化一個點類
Point point = new Point(1, 1);


// 例項化圓類
Circle circle = new Circle(point, 1);

// 作業
Person work = new Person ();


// 1.圓的面積
double area = work.getCircleArea(circle);
System.out.println("面積:" + area);




// 2.圓的周長
double length = work.getCircleLength(circle);
System.out.println("周長:" + length);


// 3.點和圓的關係
Point newPoint = new Point(2, 2);


String relation = work.relation(newPoint, circle);
System.out.println("關係:" + relation);



}
}


方式二
class  work3_1
{
public static void main(String[] args) 
{
Point p = new Point(0,0);
Circle c = new Circle(p,2);


System.out.println("面積:"+c.getS());
System.out.println("周長:"+c.getC());


Point p1 = new Point(3,3);
c.getP(p1);
}
}


class Circle
{
private Point p;
private double r;

Circle(Point p,double r){
this.p = p;
this.r = r;
}

public double getC(){
return 2*3.14*r;
}


public double getS(){
return 3.14*r*r;
}


public void getP(Point p){
double x = (p.getX() - this.p.getX()) * (p.getX() - this.p.getX()) + (p.getY() - this.p.getY()) * (p.getY() - this.p.getY());
if (x>r*r)
{
System.out.println("該點在園外");
}else if (x==r*r)
{
System.out.println("該點在園上");
}else{
System.out.println("該點在園內");
}
}
}


class Point
{
private double x;
private double y;


Point (double x,double y){
this.x = x;
this.y = y;
}


public void setX(double x){
this.x = x;
}
public double getX(){
return x;
}


public void setY(double y){
this.y = y;
}
public double getY(){
return y;
}
}








2.李曉在家裡開party,向朋友介紹家中的黃色的寵物狗【彩彩】具有兩條腿走路的特異功能。


/*
  分析:
Person:
特性:姓名
行為:
開party
介紹寵物狗
Dog:
特性:
姓名
特異功能
顏色


*/




class Dog{
String name;
String color;
String func;
Dog(){}
Dog(String name, String color, String func){

this.name = name;
this.color = color;
this.func = func;
}
}


public class Person{
String name;

Person(){}
Person(String name){

this.name = name;
}


void makeParty(){

System.out.println(name + "開party");
}

void introduce(Dog d){

System.out.println(name + "介紹家中【"+d.color+"】寵物狗【"+d.name+"】具有【"+d.func+"】的特異功能");
}

public static void main(String[] args){


Dog d = new Dog("彩彩","黃色","兩條腿獨立行走");

Person p1 = new Person("李曉");
p1.makeParty();
p1.introduce(d);

}
}






3.王梅家的荷蘭寵物豬【笨笨】跑丟了,她哭著貼尋豬啟示。


/*
  分析:
Person:
特性:姓名
行為:

貼啟示
Pig:
特性:
品種
行為:



*/




class Pig{
String brand;
String name;

Pig(){}
Pig(String brand, String name){

this.brand = brand;
this.name = name;
}

void run(){

System.out.println(name + "跑丟了");
}
}


public class Person{
String name;

Person(){}
Person(String name){

this.name = name;
}
void cry(){
System.out.println(name + "哭了");
}
void paste(){

System.out.println("貼尋豬啟示");
}

public static void main(String[] args){

Pig d = new Pig("荷蘭豬","笨笨");
d.run();

Person p1 = new Person("王梅");
p1.cry();
p1.paste();
}
}




4.富二代張三向女朋友李四介紹自己的新跑車:白色的賓利
/*
  分析:
Person:
特性:姓名
性別
行為:
介紹車
Car:
特性:
品牌
顏色




*/




class Car{
String brand;
String color;

Car(){}
Car(String brand, String color){
this.brand = brand;
this.color = color;
}
}
public class Person{
String name;
Person(){}
Person(String name){
this.name = name;
}
void introduce(Person d, Car c){
System.out.println(name + "向【"+d.name+"】介紹自己的新車【"+c.color+"】的【"+c.brand+"】");
       }
public static void main(String[] args){

Car c = new Car("賓利","白色");

Person p1 = new Person("李四");

Person p2 = new Person("張三");
p2.introduce(p1, c);

}
}