java考試編程題
public class Circle {
private double radius;//Circle類的成員變數:radius 半徑
// Circle類的方法成員:Circle( ) 構造方法,將半徑置為1;
public Circle() {
radius=1;
}
// Circle(double r) 構造方法,創建Circle對象時將半徑初始化為r;
public Circle(double r) {
radius = r;
}
// double getRadius() 獲得圓的半徑值
public double getRadius(){
return radius;
}
// double getPerimeter() 獲得圓的周長
double getPerimeter(){
return 2*Math.PI*getRadius();
}
// double getArea() 獲得圓的面積
public double getArea(){
return Math.PI*getRadius()*getRadius();
}
// void circlePrint() 將圓的半徑,周長,面積輸出到屏幕
public void circlePrint(){
System.out.println("radius="+getRadius());
System.out.println("Perimeter="+getPerimeter());
System.out.println("Area="+getArea());
}
public static void main(String args[]) {
new Circle().circlePrint();//調用默認的構造函數,如果要對半徑賦值,在括弧里賦值即可
}
}
『貳』 JAVA 考試編程題如何編寫一個矩形類
用這個肯定可以
public class Rectangle {
private float width;//長
private float height;//寬
public Rectangle(float width, float height){
this.width = width;
this.height = height;
}
//計算周長= (長+寬)*2
public float calcCircle(){
return (width + height) * 2;
}
//計算面積 長*寬
public float calcArea(){
return width * height;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
}
『叄』 java考試,求編程題步驟!!!
public class Test1 {
public static void main(String[] args) {
int sum=0;
for (int i = 1; i <=50; i++) {
if(i%2==1){
sum+=i;
}
}
System.out.println("1到50之內奇數和為:"+sum);
}
}
另外一種用while的方法:
public class Test1 {
public static void main(String[] args) {
int sum=0;
int i=1;
while(i<=50){
if(i%2==1){
sum+=i;
}
i++;
}
System.out.println("1到50之內奇數和為:"+sum);
}
}
『肆』 java面向對象程序設計期末考試編程題!急!!!
===============================第一題==============================
import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;
public class test extends Applet {
private Label label;
@Override
public void init() {
label=new Label("歡迎來到java世界!");
label.setBackground(Color.BLUE);
setBackground(Color.PINK);
add(label);
}
}
===============================第二題==============================
因為沒圖,所以自己設計的界面..
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class test implements ActionListener {
private JFrame frame;
private JLabel label;
private JTextArea jt;
private JScrollPane jsp;
private JButton show;
public test() {
frame=new JFrame("Test");
label=new JLabel("顯示內容");
jt=new JTextArea(10,20);
jsp=new JScrollPane(jt);
show = new JButton("顯示給定內容");
JPanel panel=new JPanel();
panel.add(label);
panel.add(show);
frame.add(jsp,BorderLayout.NORTH);
frame.add(panel,BorderLayout.SOUTH);
show.addActionListener(this);
show();
}
public void show(){
frame.setLocation(200, 200);
frame.setSize(300, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
label.setText(jt.getText());
}
public static void main(String[] args) {
new test().show();
}
}
『伍』 用JAVA寫編程題
import java.util.ArrayList;
import java.util.Collections;
public class TestStudent {
public double getArr(ArrayList<Student> list, String name) {
double sum = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)) {
sum = list.get(i).getMathSor() + list.get(i).getJavaSor()
+ list.get(i).getEngSor();
}
}
return sum / 3;
}
public void getHigh(ArrayList<Student> list) {
ArrayList<Double> javaSor = new ArrayList<Double>();
for (int i = 0; i < list.size(); i++) {
javaSor.add(list.get(i).getJavaSor());
}
Collections.sort(javaSor);
System.out.println("java low is " + javaSor.get(0));
System.out.println("java high is " + javaSor.get(list.size() - 1));
System.out.println("-----------------------------------");
ArrayList<Double> mathSor = new ArrayList<Double>();
for (int i = 0; i < list.size(); i++) {
mathSor.add(list.get(i).getMathSor());
}
Collections.sort(mathSor);
System.out.println("math low is " + mathSor.get(0));
System.out.println("math high is " + mathSor.get(list.size() - 1));
System.out.println("-----------------------------------");
ArrayList<Double> engSor = new ArrayList<Double>();
for (int i = 0; i < list.size(); i++) {
engSor.add(list.get(i).getEngSor());
}
Collections.sort(engSor);
System.out.println("eng low is " + engSor.get(0));
System.out.println("eng high is " + engSor.get(list.size() - 1));
}
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("01", "B", "boy", 25, 78, 78, 78));
list.add(new Student("02", "A", "boy", 23, 68, 98, 94));
list.add(new Student("03", "R", "girl", 24, 78, 78, 65));
list.add(new Student("04", "Q", "boy", 27, 78, 99, 87));
list.add(new Student("05", "F", "girl", 17, 78, 68, 78));
TestStudent testStudent = new TestStudent();
for (int i = 0; i < 5; i++) {
String name = list.get(i).getName();
double arrSor = testStudent.getArr(list, name);
System.out.println(name + "`s arrSor is " + arrSor);
}
System.out.println("-----------------------------------");
testStudent.getHigh(list);
}
}
=====================================
public class Student {
private String id;
private String name;
private String sex;
private int age;
private double mathSor;
private double javaSor;
private double engSor;
public Student() {
super();
}
public Student(String id, String name, String sex, int age, double mathSor,
double javaSor, double engSor) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.mathSor = mathSor;
this.javaSor = javaSor;
this.engSor = engSor;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getMathSor() {
return mathSor;
}
public void setMathSor(double mathSor) {
this.mathSor = mathSor;
}
public double getJavaSor() {
return javaSor;
}
public void setJavaSor(double javaSor) {
this.javaSor = javaSor;
}
public double getEngSor() {
return engSor;
}
public void setEngSor(double engSor) {
this.engSor = engSor;
}
}
============================================
兩個類,很簡單
『陸』 JAVA比較經典的考試編程題目有哪些基礎的!!
http://zpsailor.javaeye.com/admin/blogs/679770
這是我在准備面試的時候做了點基礎編程題目,你可以看看。
『柒』 兩個JAVA編程的題,考試急用!!!求大神
packagecom.ld.Demo;
importjava.util.Scanner;
publicclassTest01{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.println("輸入行數:");
intnum=scanner.nextInt();
for(inti=1;i<=num;i++){
for(intk=1;k<=num-i;k++){
System.out.print("");
}
for(intj=0;j<2*(i-1)+1;j++){
System.out.print("*");
}
System.out.println();
}
}
}
運行圖片:
『捌』 JAVA編程題~!考試用~!~急急
哦轉眼又到放暑假的時候了!看你那麼急給你寫個吧:寫兩個類一個main方法測試類
Goods為商品類SupperMacket為超市類
package cn.test.suppermacket.pojo;
public class Goods {
private String name; //商品名
private double price; //價錢
private int number; //產品數量
private int sale; //記錄商品餘下安全數
private String add; //標記產品是否上架 Y為上架N為未上架
private String showMe; //產品相關信息
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getSale() {
return sale;
}
public void setSale(int sale) {
this.sale = sale;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public String getShowMe() {
return showMe;
}
public void setShowMe(String showMe) {
this.showMe = showMe;
}
}
----------------------------------------------------SupperMacket類-------------------------------------------------------------------------
package cn.test.suppermacket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import cn.test.suppermacket.pojo.Goods;
public class SupperMacket {
private Goods goods;
private Map<String,Goods> macket=new HashMap<String,Goods>();
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public boolean append(Goods goods)
{
if(macket.get(goods.getName())!=null)
{
macket.get(goods.getName()).setNumber(macket.get(goods.getName()).getNumber()+1);
}else
{
goods.setNumber(1);
macket.put(goods.getName(), goods);
}
return true;
}
public boolean delete(String name)
{
if(macket.get(name)!=null)
{
if(macket.get(name).getNumber()==0)
{
System.out.println("已經沒貨了還不快去進貨!");
macket.get(name).setNumber(1);
}
macket.get(name).setNumber(macket.get(name).getNumber()-1);
}else
{
System.out.println("沒有這種商品!");
}
return true;
}
public void query()
{
if(macket.size()>0)
{
Iterator iter=macket.values().iterator();
for(int i=0;i<macket.size();i++)
{
Goods emp=(Goods) iter.next();
System.out.println("|-商品名-:"+emp.getName());
System.out.println("|----價錢:¥"+emp.getPrice());
System.out.println("|----數量:"+emp.getNumber());
System.out.println("|----上架情況-"+("Y".equals(emp.getAdd())?"已上架":"未上架"));
System.out.println("|----商品信息-"+emp.getShowMe());
System.out.println("----------------------------------------------------------");
}
}else
{
System.out.println("超市什麼都沒有了!");
}
}
public void doSale(String name)
{
if(macket.get(name)!=null)
{
if(macket.get(name).getNumber()<2)
{
System.out.println(macket.get(name).getName()+"最後一件售出了!");
if(macket.get(name).getNumber()==0)
{
System.out.println("已經沒貨了還不快去進貨!");
macket.get(name).setNumber(1);
}
}
macket.get(name).setNumber(macket.get(name).getNumber()-1);
}else
{
System.out.println("沒有這種商品!");
}
}
public void doAdd(String name)
{
if(macket.get(name)!=null)
{
if("Y".equals(macket.get(name).getAdd()))
{
System.out.println(macket.get(name).getName()+"已經上架");
}else
{
macket.get(name).setAdd("Y");
}
}
}
public boolean findGoods(String name)
{
if(macket.get(name)!=null)
{
return true;
}
return false;
}
}
------------------------------------------------------main主調方法類--------------------------------------------------------------------------------
package cn.test.test;
import java.util.Scanner;
import cn.test.suppermacket.SupperMacket;
import cn.test.suppermacket.pojo.Goods;
public class DemoTest {
public static void main(String[] args) {
SupperMacket macket=new SupperMacket();
Scanner sc = new Scanner(System.in);
showMune(sc,macket);
}
public static void showMune(Scanner sc,SupperMacket macket){
while(true){
System.out.println("***********************************");
System.out.println("**********超市測試菜單**************");
System.out.println("***(1)增加新的商品******************");
System.out.println("***(2)減少已有商品******************");
System.out.println("***(3)現有商品列表******************");
System.out.println("***(4)商品貨物出售******************");
System.out.println("***(5)商品上架操作******************");
System.out.println("請輸入你要的操作序號(1-5):");
int i=sc.nextInt();
switch(i){
case 1:
Goods goods=new Goods();
makeGoods(goods,sc,macket);
macket.append(goods);
break;
case 2:
System.out.println("請輸入要減少的商品名稱:");
String name=sc.next();
macket.delete(name);
break;
case 3:
macket.query();
break;
case 4:
System.out.println("請輸入要出售的商品名稱:");
name=sc.next();
macket.doSale(name);
break;
case 5:
System.out.println("將商品標記為上架:");
name=sc.next();
macket.doAdd(name);
break;
}
}
}
public static Goods makeGoods(Goods goods,Scanner sc,SupperMacket macket)
{
System.out.println("請輸入要添加的商品名稱:");
goods.setName(sc.next());
String name=goods.getName();
if(macket.findGoods(name))
{
return goods;
}
System.out.println("請輸入商品的價錢");
goods.setPrice(sc.nextDouble());
System.out.println("請輸入產品的相關信息~");
goods.setShowMe(sc.next());
return goods;
}
}