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<sum;i++){
      long age1 =s.nextLong();
      long age2 =s.nextLong();
      long count=age1+age2;
    System.out.println(count);
  }
    s.close();
  }
}

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<n-1;i++)
        System.out.printf(str+"");
    System.out.println(str);
  }
}

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<n-1;i++)
        System.out.printf(str+""+"\n");
    System.out.println(str);
  }
}

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<j;m++){
System.out.print(str.substring(1));
}
System.out.print("\n");
}
s.close();
}
}

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<n;j++) {
		int m=Math.abs(a.nextInt());
		String t=Integer.toString(m);
		l[j]=t.length();
	}
	for(int j=0;j<n;j++) {
			System.out.println(l[j]);
		}a.close();
  }
}

 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<arr.length;j++){
        for(int m=0;m<j;m++)
        if(arr[m]==arr[j])
        continue outer;
        System.out.print(arr[j]);
      }System.out.print("\n");
    }a.close();
  }
}

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<n;j++) {
  int m=in.nextInt();
  int num=m;
  String t=Integer.toString(m);
  int l=t.length();
  if(num>=0){
   char[]strarray=t.toCharArray();
   for(int i=0;i<l;i++){
    sum[j]=strarray[i]-'0'+sum[j];} }
  else {
  char[]strarray=t.toCharArray();
  for(int i=1;i<l;i++)
  {
   sum[j]=strarray[i]-'0'+sum[j];
 }
}
}
   for(int j=0;j<n;j++) {
   System.out.println(sum[j]);
    }
  }
}

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<c;i++){
    String b = a.nextLine();
    System.out.println(b.length());
  }a.close();}}

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;i<b;i++){
      int from =a.nextInt();
      int to =a.nextInt();
      if(from>to){
        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

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

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 vehi
  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用来完成大作业的。文档内容: 1 Java技术体系 1.1 Java语言 1.2 Java平台 1.3 Java应用领域 2 Java语言的技术特点 2.1 1 2.2 2 2.3 3 3 Java语言与C++的异同分析总结。 4 选用C和java语言时编程算法程序有什么不同,有什么优势和劣势。 5 自己编程学习的级别和状态。以及自己以后的编程学习的计划和想法。 6 下面3道题目中选一道,给出算法分析和程序。 1)“黄金分割数”在我们的生活中很常见,但是在不同的应用领域,要求的精度也不一样。 例如:三位小数是0.618 现在我们需要你能求出保留100位小数的黄金分割数,采用的算法为“分层计算法”: 黄金数= 1 --------------- 1+ 1 ------------- 1+ 1 ----------- 1+ 1 --------- ..... 注意,计算出的结果,如果第100位为0也需要保留。 2)已知一个数列: 5,2,4,3,7,6 那么,在这个数列中存在这样一些“连续数”,例如:5,2,4,3这个子数列排序后是连续的。同样2,4,3也是连续的,为了方便表示 我们使用下标来标识,这样,这个数列中存在以下“连续数”: [1,1] [1,4] [1,6] [2,2] [2,4] [3,3] [3,4] [4,4] [5,5] [5,6] [6,6] 这样,他就存在11个“连续数”。现在需要你在用户找出一个数组中所有的“连续数”。 要求: 1、用户输入一个整数N,表示下面数组的个数 2、用户每输入一行作为一个数组 如: 用户输入: 1 5,2,4,3,7,6 程序输出: 11 3)有一种数我们称之为幸运数,它的特点是这样的,首先,由自然数按顺序排列: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 … 这样,1比较特殊, 1为第一个幸运数,那么,我们移除掉序号能被2整除的数(注意:是序号,而不是数本身,每次移除后都重新排序)就剩下: 1 3 5 7 9 11 13 15 17 19… 3为第二个幸运数,那么我们需要去掉序号能被3(下一次是除4,然后是5,每次加1)整除的数,5 11 17...剩下: 1 3 7 9 13 15 19… 那么7为第三个幸运数,后面的幸运数,依此类推,移除之后剩下的数字都是幸运数。 现在我们需要你求出给定的m和n之间的幸运数的个数: 例如:给定1 20,那么个数为:5(5个幸运数分别是1,3,7,13,19) 现在要求用户输入两个数m和n(m<n<=1000*1000),输出幸运数的个数。 例如: 用户输入: 1 20 程序输出: 5 格式:小四,1.5倍行距

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值