java程序设计编程作业题总结(一)

1.Hello java

Output a greeting phrase "Hello, Java! "

Input

None

Output

Hello, Java! 

Hints

Please output things according to the requirements. No extra output is allowed.

The class name MUST be Main. It's case sensitive. And NO package statement is allowed, so get rid of the package statement if there is such statement.

代码如下:

public class Main{
    public static void main(String[] args){
       System.out.println("Hello, Java!");
    }
}

学习java的第一节台阶,主要是对java框架的理解以及sout的使用,然后正式开始java的学习。

2. what have you got?

You are requested to output what you got from the system console.

Input:

A line of string, until no input is found! Example:

This is the first line.

This is the second line.

...

This is the end.

Output:

What you get from the console(keyboard)

This is the first line.

This is the second line.

...

This is the end.

Hints:

On getting input from the console, you can refer to various articles on the web. For reading a line from the console, here is an example:

Scanner s = new Scanner(System.in);
if (s.hasNextLine()) {
      String line = s.nextLine();
      ......
}

You can get more info on console input/output by Google.

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    while (true) {
      if (s.hasNextLine()){
        String line = s.nextLine();
        System.out.println(line);
      }
      else
      break;
    }
  s.close();
  }
}

这次题目涉及到了hasnextline的使用,下面对hasnextline和hasnext等进行辨析:

3.A+B problem

You are requested to calculate the sum of two integral numbers a and b.

Input

A pair of number separated by the blank character. Example

5 12

Output

the sum of the input pair

17 

Hints

On getting input from the console, you can refer to various articles on the web. Here is an example,

Scanner s = new Scanner(System.in);
int age = s.nextInt();

If you want to get the whole line, you can use

String str = s.nextLine();

You can get more info on console input/output by Google.

代码如下:

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner s =new Scanner(System.in);
    long age1 = s.nextLong();
    long age2 = s.nextLong();
    long count=age1+age2;
    System.out.println(count);
    s.close();
  }
}

4.A+B problem II

You are requested to calculate the sum of two integers a and b. This time we go further. There are multiple pair of a and b in a testing group. So you need to calculate the sum several times for each testing group. Note that there may be huge number of pairs to calculate. So don't make an assumption of how many test cases.

Input

Test case count T following by each test case. Example:

3
5 12
1 1
3 -5

Output

The sum of each input pair

17
2
-2 

Hints

On geting input from the console, you can refer to various articles on the web. Here is a example:

Scanner s = new Scanner(System.in);
int age = s.nextInt();

If you want to get the whole line, you can use:

String str = s.nextLine();

You can get more info on console input/output by Google.

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s =new Scanner(System.in);
    long sum = s.nextLong();
    for(long i = 0;i

5.Repeat I

Given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s n times.

Input:
4
aabb

output:
aabbaabbaabbaabb

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s =new Scanner(System.in);
    int n = s.nextInt();s.nextLine();
    String str = s.nextLine();
    for (int i=0;i

6.Repeat II

Given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s in n lines.

Input:
4
aabb

output:
aabb
aabb
aabb
aabb

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s =new Scanner(System.in);
    int n = s.nextInt();
    String b = s.nextLine();
    String str = s.nextLine();
    for (int i=0;i

7.Repeat III

An repeat problem is that given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s n times. Now it has become a bit more complicated. There are several test cases.

Input: Total test case count T following by T cases of test instances. Example:
3
4 aabb
0 a b
3 cd

output:
aabbaabbaabbaabb

cdcdcd

代码如下:

import java.util.*;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int number = s.nextInt();
for(int i = 0; i < number; i++){
int j=s.nextInt();
String str = s.nextLine();
for(int m=0;m

8. Approximate PI

π can be computed using the following formula: \pi = 4 * (1- \frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-...+\frac{1}{n})π=4∗(1−31​+51​−71​+91​−...+n1​)

For a given number nn, write a program that displays the result of \piπ.

Input

A single line containing a odd number nn. Example:

9

Output

The result of calculated PI, keep 6 digit after the decimal point.

3.339683

Hints

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
	  Scanner in = new Scanner(System.in);
	  int n=in.nextInt();
	  double sum=0,temp=0;
	  int k=1;
	  for(int j=1;j<=n;j=j+2) {		  
		  temp=1.0/(double)j;
		  temp=temp*k;
		  sum+=temp;
		  k=-1*k;
	  }
      double PI=4*sum;
      System.out.printf("%.6f\n",PI);
  }
}

9.A+B Problem III

Description

Your task is to Calculate a + b

Too easy?!

Of course! I specially designed the problem for acm beginners.

You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same goal.

Input

The input will consist of a series of pairs of integers aa and bb, separated by a space, one pair of integers per line. Both aa and bb are within the range of 32 bit integers.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20

Sample Output

6
30
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s =new Scanner(System.in);
    while (true) {
      if(s.hasNextLine()){
        long a =s.nextLong(),b=s.nextLong();
        System.out.println(a+b);
      }else break;      
    }s.close();
  }
}

10.A+B Problem IV

Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20
0 0

Sample Output

6
30

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s =new Scanner(System.in);
    while (true) {
    long a =s.nextLong(),b=s.nextLong();
    if(a==0 && b==0)
    break;
    System.out.println(a+b);
    }s.close();}
}

11.Digit count

Given a number n, you are required to output how many digits are there.

There are several test cases for each test group.

Input

Test case count T following by each test case

Output

Output digit count of the given numbers in each group.

Sample Input

2
12
120

Sample Output

2
3

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
	Scanner a = new Scanner(System.in);
  int[] l=new int[100];
	int n=Math.abs(a.nextInt());
	for(int j=0;j

 12.Unique Digits

Given a number n, you are required to output its unique digits in appearing order. There are several test cases for each test group.

Input

Test case count T following by each test case, preceding zeros may exist. Example:
5
1212
120
0121
0000
-23

Output

12
120
012
0
23

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner a = new Scanner(System.in);
    int b = a.nextInt();
    for(int i=0;i < b;i++){
      String num = a.next();
      char[] arr = num.toCharArray();
      int j=0;
      if(arr[0]=='-')j=1;
      outer:for(;j

13.Sum of digits

Given a number n, you are required to output the sum of its digits. There are several test cases for each test group.

Input

Test case count T following by each test case. Example:
5
1211
1234
012
1111
-23

Output


5
10
3
4
5

代码如下:

import java.util.*;  
public class Main {
  public static void main(String[] args) {
   int[] sum=new int[10];
   for(int i=0;i<10;i++)sum[i]=0;
   Scanner in = new Scanner(System.in);
   int n=in.nextInt();
   for(int j=0;j=0){
   char[]strarray=t.toCharArray();
   for(int i=0;i

14.String Length

Your are to output the length of a string. There are several test cases for each test group.

Input: Test case count T following by each test case. Example:
4
asdf
avc asd as
00100

Output:
4
8
10
5

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner a = new Scanner(System.in);
  int c = Integer.parseInt(a.nextLine());
  for(int i=0;i

15.foo bar baz

You are given two numbers "from"(inclusive) and "to"(inclusive). And you are expected to create an application that loops from "from" to "to" and prints out each value on a separate line, except print out "foo" for every multiple of 3, "bar" for every multiple of 5, and "baz" for every multiple of 7.

If "From" is greater than "to", then output a blank line.

For example: if from=1 and to=16, then it will give the following output:
1
2
3 foo
4
5 bar
6 foo
7 baz
8
9 foo
10 bar
11
12 foo
13
14 baz
15 foo bar
16

Input: Total test case count T following by T cases of test instances. Example:
3
4 5
6 10
3 -1

output:
4
5 bar
6 foo
7 baz
8
9 foo
10 bar

Hint -- The % operator calculates an integer remainder.

代码如下:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner a=new Scanner(System.in);
    int b = a.nextInt();
    for(int i=0;ito){
        System.out.print("\n");
        continue;
      }
      for(int count = from;count>=from&&count<=to;count++){
        if(count%3==0&&count%5!=0&&count%7!=0)System.out.println(count+" foo");
        else if(count%3!=0&&count%5==0&&count%7!=0)System.out.println(count+" bar");
        else if(count%3!=0&&count%5!=0&&count%7==0)System.out.println(count+" baz");
        else if(count%3==0&&count%5==0&&count%7!=0)System.out.println(count+" foo bar");
        else if(count%3==0&&count%7==0&&count%5!=0)System.out.println(count+" foo baz");
        else if(count%5==0&&count%7==0&&count%3!=0)System.out.println(count+" bar bar");
        else if(count%3==0&&count%5==0&&count%7==0)System.out.println(count+" foo bar baz");
        else System.out.println(count);
      }
    }
a.close();
  } 
}

16.Version 1: No Information Hiding

java程序设计编程作业题总结(一)_第1张图片

In this version of the Vehicle class, you will leave the attributes public so that the test program TestVehicle1 will have direct access to them.

  1. Create a class Vehicle that implements the above UML diagram.
    1. Include two public attributes: load "the current weight of the vehicle's cargo" and maxLoad "the vehicle's maximum cargo weight limit".
    2. Include one public constructor to set the maxLoad attribute.
    3. Include two public access methods: getLoad to retrieve the load attribute and getMaxLoad to retrieve the maxLoad attribute.

    Note that all of the data are assumed to be in kilograms.

  2. The test program Main.java is preset. So just submit your Vehicle class. If everything is ok, The output will be as expected. You can read the test program. Notice that the program gets into trouble when the last box is added to the vehicle's load because the code does not check if adding this box will exceed the maxLoad.
  3. You can get the test code and run the Main class locally. The output generated should be:
    Creating a vehicle with a 10,000kg maximum load. Add box #1 (500kg) Add box #2 (250kg) Add box #3 (5000kg) Add box #4 (4000kg) Add box #5 (300kg) Vehicle load is 10050.0 kg 
    

Note: you need to submit the Vehicle class only. The test Main class is ready as is shown. What's more, do not make the Vehicle class public

  测试输入 期待的输出 时间限制 内存限制

额外进程

测试用例 1 以文本方式显示
以文本方式显示
  1. Creating a vehicle with a 10,000kg maximum load.↵
  2. Add box #1 (500kg)↵
  3. Add box #2 (250kg)↵
  4. Add box #3 (5000kg)↵
  5. Add box #4 (4000kg)↵
  6. Add box #5 (300kg)↵
  7. Vehicle load is 10050.0 kg↵
1秒 无限制 0

 代码如下:

/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
public class Main {
  public static void main(String[] args) {
    // Create a vehicle that can handle 10,000 kilograms weight
    System.out.println("Creating a vehicle with a 10,000kg maximum load.");
    Vehicle vehicle = new Vehicle(10000.0);
    // Add a few boxes
    System.out.println("Add box #1 (500kg)");
    vehicle.load = vehicle.load + 500.0;
    System.out.println("Add box #2 (250kg)");
    vehicle.load = vehicle.load + 250.0;
    System.out.println("Add box #3 (5000kg)");
    vehicle.load = vehicle.load + 5000.0;
    System.out.println("Add box #4 (4000kg)");
    vehicle.load = vehicle.load + 4000.0;
    System.out.println("Add box #5 (300kg)");
    vehicle.load = vehicle.load + 300.0;
    // Print out the final vehicle load
    System.out.println("Vehicle load is " + vehicle.getLoad() + " kg");
  }
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
class Vehicle{
  double load;double maxLoad;
  public double getLoad(){
    return load;
  }
  public double getMaxLoad(){
    return maxLoad;
  }
  public Vehicle(double maxload){
    maxLoad=maxload;
  }
}

17.Version 2: Basic Information Hiding

java程序设计编程作业题总结(一)_第2张图片

Version 2: Basic Information Hiding

To solve the problem from the first version, you will hide the internal classdata (load and maxLoad) and provide a method, addBox, to perform the proper checking that the vehicle is not being overloaded.

  1. Create a class Vehicle that implements the above UML diagram.

    You may wish to copy the Vehicle.java file you created in version #1.

    1. Modify the load and maxLoad attributes to be private.
    2. Add the addBox method. This method takes a single argument, which is the weight of the box in kilograms. The method must verify that adding the box will not violate the maximum load. If a violation occurs the box is rejected by returning the value of false; otherwise the weight of the box is added to the vehicle load and

你可能感兴趣的:(java,java)