㈠ java面向對象程序設計一道作業題 程序設計求解答 希望用簡單的方法謝謝
自定義類MyNumber:
importjava.util.ArrayList;
importjava.util.List;
publicclassMyNumber{
publicint[]delZero(int[]arr){
int[]newArr=newint[arr.length];
for(inti=0;i<arr.length;i++){
//刪除所有含0的元素(0,10,20......)
//if(arr[i]==0&&arr[i]%10==0){
//刪除所有為0的元素
if(arr[i]==0){
arr=delAnyPosition(arr,i);
}
}
returnarr;
}
publicstaticint[]delAnyPosition(int[]arr,intposition){
//判斷是否合法
if(position>=arr.length||position<0){
returnnull;
}
int[]res=newint[arr.length-1];
for(inti=0;i<res.length;i++){
if(i<position){
res[i]=arr[i];
}else{
res[i]=arr[i+1];
}
}
returnres;
}
}
㈡ 7-1 jmu-Java-03面向對象基礎-04-形狀-繼承(15 分)
自己寫的,也才學不容易還望採納
import java.util.*;
abstract class Shape{
final static double PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
public static double sumAllArea(Shape []shapes) {
double x=0;
for(Shape e:shapes) {
x+=e.getArea();
}
return x;
}
public static double sumAllPerimeter(Shape []shapes) {
double y=0;
for(Shape e:shapes) {
y+=e.getPerimeter();
}
return y;
}
}
class Rectangle extends Shape{
private int width;
private int length;
public double getPerimeter() {
return 2*(width+length);
}
public double getArea() {
return width*length;
}
public Rectangle(int width,int length) {
this.width=width;
this.length=length;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
class Circle extends Shape{
private int radius;
public double getPerimeter() {
// TODO Auto-generated method stub
return 2*PI*radius;
}
public double getArea() {
// TODO Auto-generated method stub
return PI*radius*radius;
}
public Circle(int radius) {
this.radius=radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
n=in.nextInt();
Shape shapes[]=new Shape[n];
for(int i=0;i<n;i++) {
String s=in.next();
switch(s) {
case "rect":
int width,length;
width=in.nextInt();
length=in.nextInt();
shapes[i]=new Rectangle(width, length);
break;
case "cir":
int radius;
radius=in.nextInt();
shapes[i]=new Circle(radius);
break;
}
}
System.out.println(Shape.sumAllPerimeter(shapes));
System.out.println(Shape.sumAllArea(shapes));
System.out.println(Arrays.toString(shapes));
for(int i=0;i<n;i++) {
System.out.println(shapes[i].getClass()+","+shapes[i].getClass().getSuperclass());
}
}
}
㈢ 有一道 java面向對象編程題 求大家幫我寫一下 面向對象基礎 看到迷茫
package employee;
public class Employee {
//員工私有屬性
private String name; //員工姓名
private int age; //員工年齡
private String position; //員工職位
private int salary; //工資
/**
* 給所有的屬性指定初始值
* @param name
* @param age
* @param position
* @param salary
*/
public Employee(String name,int age,String position,int salary){
this.name = name;
this.age =age;
this.position = position;
this.salary = salary;
}
/**
* 給name屬性賦值
* @param name
*/
public void setName(String name){
this.name = name;
}
public String getName() {
return name;
}
/**
* 給age屬性賦值
* @param age
*/
public void setAge(int age){
if(age<18){
this.age=18;
System.out.println("當年齡無效時,默認為18");
}else{
this.age =age;
}
}
public int getAge() {
return age;
}
/**
* 給position屬性賦值
* @param position
*/
public void setPosition(String position){
if(position.equals("售後服務") || position.equals("銷售員") ){
this.position = position;
}else{
this.position = "售後服務";
System.out.println("輸入不符合要求,默認為售後服務");
}
}
public String getPosition() {
return position;
}
/**
* 給員工工資賦值
* @param salary
*/
public void setSalary(){
if(age>=18 && age<=20){
this.salary = 1000;
}else if(age>=21 && age<=25){
this.salary = 1500;
}else if(age>=26 && age<=30){
this.salary = 2000;
}else if(age>=31 && age<=40){
this.salary = 3000;
}else if(age>=41 && age<=50){
this.salary = 3500;
}else if(age>=51){
this.salary = 4000;
}else{
System.out.println("沒有設置年齡或者年齡無效");
}
}
public int getSalary() {
return salary;
}
}
測試類
package employee;
import java.util.Scanner;
public class TestEmployee {
public static void main(String[] args){
Employee emp1 = new Employee(null, 0, null, 0) ;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第一個員工姓名");
//獲取輸入的名字
String name1 = sc.next();
emp1.setName(name1);
System.out.println("請輸入第一個員工年齡");
//獲取輸入的年齡
int age1 = sc.nextInt();
emp1.setAge(age1);
System.out.println("請輸入第一個員工職位");
//獲取輸入的職位
String position1 = sc.next();
emp1.setPosition(position1);
emp1.setSalary();
System.out.println("---------------------------------");
System.out.println("員工1姓名為:"+emp1.getName());
System.out.println("年齡:"+emp1.getAge());
System.out.println("工作為:"+emp1.getPosition());
System.out.println("工資為:"+emp1.getSalary());
}
}
第一次回答問題,玩玩而已但是還是希望採納
㈣ java面向對象基礎題目(字元串)
選B,change函數的入口參數為str則只修改傳入參數,若在函數體內使用「this.str=str」則發生互換,第二個直接修改了引用值,導致發生改變。
㈤ java面向對象選擇題
C
A 沒有返回值
B 不能接受同類型的參數返回不同的類型
D 返回值和類型不匹配
㈥ JAVA編程題:計算整型數之和(面向對象)
public class Calculator {
public double add(double num1, double num2){
return num1 + num2;
}
public double minus(double num1, double num2){
return num1 - num2;
}
public double multiple(double num1, double num2){
return num1 * num2;
}
public double divide(double num1, double num2){
if(num2 != 0){
return num1/num2;
}
return -1;//除數為0,錯誤
}
}
㈦ java面向對象的問題
類,用於描述多個對象的共同特徵,它是對象的模板。
對象,用於描述現實中的個體,它是類的實例。
java面向對象的問題Cat c = new Cat();
new Cat()代表對象,而c代表這個對象的引用,那麼為什麼有人說c是這個對象的實例呢?
你基本概念理解不透徹呀。
類,用於描述多個對象的共同特徵,它是對象的模板。
對象,用於描述現實中的個體,它是類的實例。
對面向對象有了了解之後,我們來說說在具體問題中如何使用面向對象去分析問題,和如何使用面向對象。
我們把大象裝冰箱為例進行分析。
在針對具體的需求,可以使用名詞提煉的辦法進行分析,尋找具體的對象。
需求:把大象裝冰箱里
對象:大象、冰箱
分三步:
1、打開冰箱門
2、將大象裝進去
3、關閉冰箱門
分析發現打開、裝、關閉都是冰箱的功能。即冰箱對象具備如下功能:
冰箱打開
冰箱存儲
冰箱關閉
用偽代碼描述,上述需求中有兩個具體的事物 大象 和 冰箱
描述大象:
class 大象
{
}
描述冰箱
class冰箱
{
void 打開(){}
void 存儲(大象){}
void 關閉(){}
}
當把具體的事物描述清楚之後,需要使用這些具體的事物,Java使用具體的事物,需要通過new關鍵字來創建這個事物的具體實例。
所以當你創建Cat對象的時候,既可以說c是Cat對象的引用也可以說是Cat對象的實例。
l汽車類
publicclassCar {
String color;
intnumber;
voidrun() {
System.out.println(color+ ":"+ number);
}
}
通過代碼的描述,知道類的真正意義就是在描述事物。屬性和功能統稱為事物中的成員。
事物的成員分為兩種:成員屬性和成員功能。
成員屬性在代碼中的體現就是成員變數
成員功能在代碼中的體現就是成員方法
把寫好的代碼測試一下。需要一個可以獨立運行類。
創建對象的格式:
類名 對象名 = new 類名();
測試類
publicclassCarDemo {
publicstaticvoidmain(String[] args) {
/*
* 測試:Car類中的run方法。
*/
// 1,創建Car的對象。給對象起個名字。
Car c = newCar();// c是類類型的變數。c指向了一個具體的Car類型的對象。
// 2,通過已有的對象調用該對象的功能。格式:對象.對象成員;
// 3,可以該對象的屬性賦值。
c.color= "red";
c.number= 4;
c.run();
}
}
如果你還不懂,你可以私信我。
㈧ Java面向對象程序設計考試題目 類的定義 繼承 創建對象 構造方法
public class Geometry {
public Geometry(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
private int width, height;
}
public class Cube extends Geometry {
public Cube(int w, int h) {
super(w, h);
}
public Cube(int a, int b, int c) {
super(a, b);
height = c;
}
public void setHeight(int h) {
height = h;
}
public int volumn() {
return area() * height;
}
private int height;
}
public class User {
public static void main(String []args) {
Cube cube1 = new Cube(1,2,3);
Cube cube2 = new Cube(4, 5);
cube2.setHeight((int) (Math.random() * 10) + 1);//avoid zero height
System.out.println("Cube 1 area: " + cube1.area() + " volumn: " + cube1.volumn());
System.out.println("Cube 2 area: " + cube2.area() + " volumn: " + cube2.volumn());
}
}
㈨ JAVA面向對象題。
這個是我剛剛寫的,可以直接運行的。應該能夠滿足你的需求吧。
public class Circle {
private float r;
private final static double PI = Math.PI;
Circle()
{
this.r = 1;
}
Circle(float radius)
{
this.r = radius;
}
private double getPerimeter()
{
return PI*2*r;
}
private double getArea()
{
return PI*r*r;
}
private double getRadius()
{
return r;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle myCircle = new Circle(3);
System.out.println("Radius is "+myCircle.getRadius()+" Perimeter is "+myCircle.getPerimeter()+" Area is "+myCircle.getArea());
}
}
㈩ java面向對象程序設計練習題 求解答
Java面向對象程序設計復習題
一、選擇題
1、下列哪個是Java中的關鍵字( C )。
A、run B、Integer C、default D、implement 2、下面關於Java.applet.Applet和其祖先類的描述語句哪個不對( B )。 A、Applet是Container的一種 B、Applet是Window的一種 C、Applet是Component的一種 D、Applet是Panel的一種 3、下列類 DataOutputStream的構造方法正確的是( A )。
A、new dataOutputStream(new FileOutputStream(「out.txt」)); B、new dataOutputStream(「out.txt」);
C、new dataOutputStream(new writer(「out.txt」)); D、new dataOutputStream(new FileWriter(「out.txt」)); 4、在switch(表達式)語句中,表達式的類型不能為( C )。 A、byte B、char C、long D、int 5、在介面MouseMotionListener中方法正確的是( A )。 A、Public void mouseDragged(MouseEvent) B、Public boolean mouseDragged(MouseEvent) C、Public void mouseDragged(MouseMotionEvent) D、Public boolean MouseDragged(MouseMotionEvent) 6、下面是一些異常類的層次關系 Java.lang.Exception
Java.lang.RuntimeException
Java.lang.IndexOutOfBoundsException
Java.lang. Java.lang.
假設有一個方法X,能夠拋出兩個異常,Array Index和String Index異常,假定方法X中沒有try-catch語句,下面哪個答案是正確的。( B )
A、方法X應該聲明拋棄和StringIndexOutOfBounds Exception。
B、如果調用X的方法捕獲IndexOutOfBoundsException,則ArrayIndexOutOfBounds Exception和都可以被捕獲。 C、如果方法X聲明拋棄IndexOutOfBoundsException,則調用X的方法必須用Try-catch語句
捕獲。
D、方法X不能聲明拋棄異常。
7、現有一變數聲明為boolean aa;下面賦值語句中正確的是( D )。 A、aa=0 B、aa=True C、aa="true" D、aa=false
8、某類Example的main()方法參數為args,當輸入數據Java Example cat時,args[0]的值為( A )。
A、cat B、Java C、example D、null
9、String s1=new String(「Java」);String s2=new String(s1)則下列哪個說法是正確的( C )。 A、表達式s1==s2為真
B、s1和s2是同一個對象
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
C、表達式s1.equals(s2)為真 D、以上均不對 10、類定義如下 class Foo{
public static void main(String args[]){ String s;
System.out.println("s="+s); } }
則下列結論正確的是( C )。
A、有輸出結果,且為空 B、沒有輸出結果
C、編譯錯誤 D、有輸出結果,且不為空
11、下列哪個不是Java的保留字( D )。
A、float B、class C、extends D、virtual 12、下列符號中不能作為Java標識符的是( D )。
A、abc B、$str1 C、_pore D、45six 13、方法methodA定義如下:
returnType methodA(byte x,double y){ return (short)x/y*2; }
則返回值returnType為( C )。
A、byte B、double C、short D、int 14、如果float f=4.2F;Float g=new Float(4.2F); Double d=new Double(4.2);則下列選項正確的是( B )。
A、f==g B、f==g.floatValue() C、d==f D、d.equals(f) 15、下列二維數組定義中錯誤的是( A )。 A、int a[][]=new int[][]; B、int []a[]=new int[10][10]; C、int a[][]=new int[10][10]; D、int [][]a=new int[10][10];
16、關於下列語句哪個答案是正確的( D )。 System.out.println(4|7);
A、4 B、5 C、6 D、7
17、下面哪一個AWT組件可以有菜單欄MenuBar( A )。 A、Java.awt.Frame B、Java.awt.Window C、Java.awt.Applet D、Java.awt.Panel
18、下列哪個方法用於創建並開始一個新的線程( B )。 A、run(); B、start();
C、execute(); D、run(Runnable r);
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
19、如果有Boolean a=new Boolean(「yes」),則a.booleanValue()值為( D )。 A、yes B、「yes」 C、true D、false 20、以下類 DataOutputStream的構造方法正確的是( C )。 A、new dataInputStream(「in.txt」);
B、new dataInputStream(new file(「in.txt」));
C、new dataInputStream(new FileInputStream(「in.txt」));
D、new dataInputStream(new FileWriter(「in.txt」));
21、編譯Java Application 源程序文件將產生相應的位元組碼文件,這些位元組碼文件的擴展名為( B )。
A、.Java B、.class C、.html D、.exe
22、設 x = 1 , y = 2 , z = 3,則表達式 y+=z--/++x 的值是( A )。 A、3 B、3.5 C、4 D、5
23、在Applet表面輸出文字時,可以選擇不同的顏色,但是忘記了設置顏色的方法,應該首先在哪個類裡面尋找( D )。 A、Java .awt.Applet B、Java.awt.Panel C、Java.applet.Applet D、Java.awt.Component 24、類Cycle的main()方法為:
public static void main(String args[]){ System.out.println(args[0]); }
則運行時如果命令行語句為Java Cycle one two three,則輸出結果為( B )。 A、Cycle B、one C、two D、three 25、下面哪一個是Thread類中的靜態方法( D )。
A、start() B、stop() C、run() D、sleep(long m) 26、關於下列語句哪個答案是正確的( A )。 if(5&7&&5|2)System.out.println(「true」);
A、不能編譯成功 B、可以編譯成功,輸出true C、可以編譯成功,但無輸出 D、以上均不對 27、聲明公用的abstract方法的正確格式是( C )。 A、public abstract void add() {} B、public abstract add();
C、public abstract void add(); D、public virtual add();
28、下列程序結果正確的是( B )。 public class Test {
public static void main (String args []) { Ad a1=new Ad();
System.out.println(a1.add(1)); Ad a2=new Ad();
System.out.println(a2.add(2)); } }
附上出處鏈接:http://wenku..com/link?url=m6mQf1x9q9-sjj17tqEy95VAcc48dBTwB_1I_