第一種:
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("第一個數:");
inta=scn.nextInt();
System.out.print("操作(+,-,*,/):");
Stringoption=scn.next();
System.out.print("第二個數:");
intb=scn.nextInt();
intresult=0;
if("+".equals(option)){
result=a+b;
}
if("-".equals(option)){
result=a-b;
}
if("*".equals(option)){
result=a*b;
}
if("/".equals(option)){
result=a/b;
}
System.out.println("結果:"+result);
}
第二種:
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("請輸入第一個數:");
intoNumFirst=scn.nextInt();
System.out.print("請輸入操作方式(+,-,*,/):");
Stringoption=scn.next();
System.out.print("請輸入第二個數:");
intoNumSecond=scn.nextInt();
intresult=0;
switch(option){
case"+":
result=oNumFirst+oNumSecond;
break;
case"-":
result=oNumFirst-oNumSecond;
break;
case"*":
result=oNumFirst*oNumSecond;
break;
case"/":
try{
result=oNumFirst/oNumSecond;
}catch(ArithmeticExceptione){
e.printStackTrace();
System.out.println("除數不能為0");
}
break;
}
System.out.println("結果為:"+result);
}
第三種:
/**
*計算器類
*CreatedbyHDLon2016/11/30.
*/
publicclassOptioner{
privateintoNumFirst;//第一個數
privateintoNumSecond;//第二個數
publicintgetoNumFirst(){
returnoNumFirst;
}
publicvoidsetoNumFirst(intoNumFirst){
this.oNumFirst=oNumFirst;
}
publicintgetoNumSecond(){
returnoNumSecond;
}
publicvoidsetoNumSecond(intoNumSecond){
this.oNumSecond=oNumSecond;
}
publicOptioner(){
}
publicOptioner(intoNumFirst,intoNumSecond){
this.oNumFirst=oNumFirst;
this.oNumSecond=oNumSecond;
}
publicintgetReuslt(){
return0;
}
}1617181920212223242526272829
/**
*加法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()+getoNumSecond();
}
}
/**
*減法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()-getoNumSecond();
}
}
/**
*乘法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()*getoNumSecond();
}
}
/**
*除法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
if(getoNumSecond()==0){
System.out.println("除數不能為0");
return-1;
}else{
returngetoNumFirst()/getoNumSecond();
}
}
}
測試
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("請輸入第一個數:");
intoNumFirst=scn.nextInt();
System.out.print("請輸入操作方式(+,-,*,/):");
Stringopt=scn.next();
System.out.print("請輸入第二個數:");
intoNumSecond=scn.nextInt();
Optioneroptioner=null;
switch(opt){
case"+":
optioner=newAddOptioner();
break;
case"-":
optioner=newSubOptioner();
break;
case"*":
optioner=newMulOptioner();
break;
case"/":
optioner=newDivOptioner();
break;
}
optioner.setoNumFirst(oNumFirst);
optioner.setoNumSecond(oNumSecond);
System.out.println("結果為:"+optioner.getReuslt());
}
2. java面向對象編程題目。要求用抽象類和介面
//abstract Shape形狀類
publicabstractclassShape{
abstractdouble area();
abstractdouble perimeter();
}
//Rectangle繼承Shape類
{
private double width;
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
private double height;
Rectangle(){
}
Rectangle(double width,double height){
this.width=width;
this.height=height;
}
public double area(){
return width*height;
}
public double perimeter(){
return (width+height)*2;
}
}
//Circle類繼承抽象類Shape
publicclassCircleextendsShape{
private final double PI=3.14;
private double radius;
Circle(){
}
Circle(double radius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
public doublearea(){
returnradius*radius*PI;
}
public doubleperimeter(){
returnradius*2*PI;
}
}
/Triangle類繼承抽象類Shape
{
private double di;
private double high;
Triangle(){
}
Triangle(double di,double high){
this.di=di;
this.high=high;
}
publicdoublegetDi(){
returndi;
}
publicvoidsetDi(doubledi){
this.di=di;
}
publicdoublegetHigh(){
returnhigh;
}
publicvoidsetHigh(doublehigh){
this.high=high;
}
public doublearea(){
return di*high*1/2;
}
public doubleperimeter(){
return di*3;//限等邊三角形
}
}
//定義介面Shape
public interfaceShape{
publicdouble area();
publicdouble perimeter();
}
//Rectangle類實現介面Shape
{
private double width;
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
private double height;
Rectangle(){
}
Rectangle(double width,double height){
this.width=width;
this.height=height;
}
public double area(){
return width*height;
}
public double perimeter(){
return (width+height)*2;
}
}
//Circle類實現介面Shape
{
private final double PI=3.14;
private double radius;
Circle(){
}
Circle(double radius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
public doublearea(){
returnradius*radius*PI;
}
public doubleperimeter(){
returnradius*2*PI;
}
}
//Triangle類實現介面Shape
{
private double di;
private double high;
Triangle(){
}
Triangle(double di,double high){
this.di=di;
this.high=high;
}
publicdoublegetDi(){
returndi;
}
publicvoidsetDi(doubledi){
this.di=di;
}
publicdoublegetHigh(){
returnhigh;
}
publicvoidsetHigh(doublehigh){
this.high=high;
}
public doublearea(){
return di*high*1/2;
}
public doubleperimeter(){
return di*3;//限等邊三角形
}
}
//測試類ShapeTest
publicclassShapeTest{
publicstaticvoidmain(String[]args){
Circlec1=newCircle(3);//圓類有參初始化
Circlec2=newCircle();//圓類無參初始化
c2.setRadius(3);//初始化c2的半徑
Squares1=newSquare(3);//方形類有參初始化
Squares2=newSquare();//方形類無參初始化
s2.setSide(5);//初始化s2的邊長
Trianglet1=newTriangle(5,6);//三角類有參初始化
Trianglet2=newTriangle();//三角類無參初始化
t2.setDi(2);//初始化t2的底
t2.setHigh(6);//初始化t2的高
print("c1的面積:"+c1.area()+" c2的面積"+c2.area());
print("c1的周長:"+c1.perimeter()+" c2的周長"+c2.perimeter());
print("s1的面積:"+s1.area()+" s2的面積"+s2.area());
print("s1的周長:"+s1.perimeter()+" s2的周長"+s2.perimeter());
print("t1的面積:"+t1.area()+" t2的面積"+t2.area());
print("t1的周長:"+t1.perimeter()+" t2的周長"+t2.perimeter());
}
//定義靜態列印方法
public static void print(Object object){
System.out.println(object);
}
}
//列印結果
c1的面積:28.26 c2的面積28.26
c1的周長:18.84 c2的周長18.84
s1的面積:9.0 s2的面積25.0
s1的周長:12.0 s2的周長20.0
t1的面積:15.0 t2的面積6.0
t1的周長:15.0 t2的周長6.0
3. 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;
}
}
4. 有一道 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());
}
}
第一次回答問題,玩玩而已但是還是希望採納
5. 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_
6. java面向對象編程題目
public class Rectangle
{
private double width;
private double height;
//無參構造器
public Rectangle()
{}
//有參構造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//屬性的get和set方法定義
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return this.width;
}
public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
//計算周長的方法
private double getPerimeter()
{
return (width+height)*2;
}
//計算面積的方法
private double getArea()
{
return width*height;
}
public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
}
}
7. 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());
}
}
8. Java面向對象程序設計.哪位JAVA高手,幫我做一下下面的編程試題...
第一題
public class AirCondition extends Device {
private String dName;//空調名稱
private String address;//空調廠家
private double price;//空調價格
private int temperature;//空調的溫度
public AirCondition(String dName, String address,double price,int temperature) {
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
//給空調升溫temp度
public void raiseTemp (int temp) {
this.temperature += temp;
}
//給空調降溫temp度
public double lowerTemp(int temp) {
return this.temperature -= temp;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public String toString() {
StringBuffer r = new StringBuffer();
r.append("設備名稱: " + getdName() + "\n");
r.append("出廠廠家: " + getAddress() + "\n");
r.append("價格: " + getPrice() + "\n");
r.append("初始溫度: " + getTemperature() + "\n");
return r.toString();
}
}
第二題
import java.io.DataInputStream;
import java.io.FileInputStream;
public class Score {
private Object[][] data = new Object[10][2];//存放學好和密碼
public static void main(String[] args) {
new Score().readData();
}
//讀取數據
public void readData(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("studentdata"));
for(int i = 0; i < data.length; i++){
data[i][0] = dis.readUTF();//學好
data[i][1] = dis.readInt();//密碼
}
//調用計算
calc();
} catch (Exception e) {
e.printStackTrace();
}
}
//計算
public void calc(){
int total = 0;
double avg = 0;
int max = 0;
int min = 0;
Object[] temp;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length - 1; j++) {
if((Integer)(data[j][1]) > (Integer)(data[j + 1][1])){
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i < data.length; i++) {
total += (Integer)data[i][1];
System.out.println("學號: " + data[i][0] + "\t" + "分數: " + data[i][1]);
}
avg = total / data.length;//平均
min = (Integer)data[0][1];
max = (Integer)data[data.length - 1][1];
System.out.println("最高分: " + max);
System.out.println("最低分: " + min);
System.out.println("平均分: " + avg);
}
}
9. java面向對象編程習題,急求答案,用MyEclipse編寫 為「無名的粉」寫一個類:class
public class WuMingFen {
private String theMa; //面碼
private int quantity;//粉的分量(兩)
private boolean likeSoup;//是否帶湯
public WuMingFen(){
this.theMa = "酸辣";
this.quantity = 2;
this.likeSoup = true;
}
public WuMingFen(String theMa,int quantity){
this.theMa = theMa;
this.quantity = quantity;
}
public WuMingFen(String theMa,int quantity,boolean likeSoup){
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = likeSoup;
}
public void check(){
System.out.println("面碼:"+this.theMa);
System.out.println("粉的分量(兩):"+this.quantity);
System.out.println("是否帶湯:"+this.likeSoup);
}
//驗證
public static void main(String[] args){
WuMingFen f1 = new WuMingFen("牛肉",3,true);
WuMingFen f2 = new WuMingFen("牛肉",2);
WuMingFen f3 = new WuMingFen();
f1.check();
f2.check();
f3.check();
}
}
10. 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,錯誤
}
}