1.重复打印
给定m组数,每组输入一个数字n和一个字符串s,表示重复n次输出该字符串s,分别给出m组数字的输出。
例子:
输入:
3
4 aabb
0 a b
3 cd
输出:
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.trim());
}
System.out.print("\n");
}
s.close();
}
}
2.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. Example:
2
12
120
Output:
2
3
代码:
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 count = 0;
int j = s.nextInt();
while(j>=1)
{
count++;
j = j / 10;
}
System.out.println(count);
}
s.close();
}
}
3.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 s = new Scanner(System.in);
int number = s.nextInt();
for(int i = 0; i < number; i++)
{
String num = s.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");
}
s.close();
}
}
4.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
Example:
3
4 5
6 10
3 -1
output:
4
5 bar
6 foo
7 baz
8
9 foo
10 bar
代码:
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 a = s.nextInt();
int b = s.nextInt();
if(a>b)
{
System.out.print("\n");
continue;
}
for(int count = a;count>=a&&count<=b;count++)
{
if(count%3 == 0) System.out.println(count+" foo");
else if (count%5 == 0) System.out.println(count+" bar");
else if(count%7 == 0) System.out.println(count+" baz");
else System.out.println(count);
}
}
s.close();
}
}
5.v1: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.
Include two public attributes: load “the current weight of the vehicle’s cargo” and maxLoad “the vehicle’s maximum cargo weight limit”.
Include one public constructor to set the maxLoad attribute.
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: The test Main class is ready as is shown. you can add it to your code.
Preset Code
Prepend Code
/ 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 /
代码:
import java.util.*;
class Vehicle {
double load;
double maxLoad;
public double getLoad()
{
return load;
}
public double getMaxLoad() {
return maxLoad;
}
public Vehicle(double max_load)
{
maxLoad = max_load;
}
}
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");
}
}
6.V2: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.
• Modify the load and maxLoad attributes to be private.
• 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 the method returns true.
• Hint: you will need to use an “if” statement. Here is the basic form of the conditional form:
if ( ) { * } else { * }
• Note that all of the data are assumed to be in kilograms.
2, Read the Main.java code. Notice that the code can not modify the load attribute directly, but now must use the addBox method. This method returns a true or false value which is printed to the screen.
3, Compile the Vehicle and Main classes locally.
4, Run the Main class. The output generated should be something like these:
• Creating a vehicle with a 10,000kg maximum load. Add box #1 (500kg) : true Add box #2 (250kg) : true Add box #3 (5000kg) : true Add box #4 (4000kg) : true Add box #5 (300kg) : false Vehicle load is 9750.0 kg
Note:The test Main class is ready as is shown. you can add it to your code.
Preset Code
Prepend Code
/ 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.addBox(500.0));
System.out.println("Add box #2 (250kg) : " + vehicle.addBox(250.0));
System.out.println("Add box #3 (5000kg) : " + vehicle.addBox(5000.0));
System.out.println("Add box #4 (4000kg) : " + vehicle.addBox(4000.0));
System.out.println("Add box #5 (300kg) : " + vehicle.addBox(300.0));
System.out.println("Add box #5 (250kg) : " + vehicle.addBox(250.0));
// Print out the final vehicle load
System.out.println(“Vehicle load is " + vehicle.getLoad() + " kg”);
} }
/ PRESET CODE END - NEVER TOUCH CODE ABOVE /
代码:
import java.util.*;
class Vehicle {
private double load;
private double maxLoad;
public double getLoad()
{
return load;
}
public double getMaxLoad() {
return maxLoad;
}
public Vehicle(double max_load)
{
maxLoad = max_load;
}
public boolean addBox(double weight)
{
if((load+weight)>getMaxLoad()) return false;
else
{
load += weight;
return true;
}
}
}
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.addBox(500.0));
System.out.println("Add box #2 (250kg) : " + vehicle.addBox(250.0));
System.out.println("Add box #3 (5000kg) : " + vehicle.addBox(5000.0));
System.out.println("Add box #4 (4000kg) : " + vehicle.addBox(4000.0));
System.out.println("Add box #5 (300kg) : " + vehicle.addBox(300.0));
System.out.println("Add box #5 (250kg) : " + vehicle.addBox(250.0));
// Print out the final vehicle load
System.out.println("Vehicle load is " + vehicle.getLoad() + " kg");
} }
7.v3:Change Internal Representati
Now suppose that you were going to write some calculations that determine the wear on the vehicle’s engine and frame. These calculations are easier if the weight of the load is measured in newtons.
1, Create a class Vehicle that implements the above UML diagram.
You may wish to copy the Vehicle.java file you created in version #2.
• Modify the constructor, getLoad, getMaxLoad, and addBox methods to use a conversion from kilograms (the parameter weight measurement) to newtons (the instance variable measurement). You might want to use the following private methods:
private double kiloToNewts(double weight) { return (weight * 9.8); } private double newtsToKilo(double weight) { return (weight / 9.8); }
Note that now the internal data of the vehicle objects is in newtons and the external data (passed between methods) is still in kilograms.
2, Read the preset Main.java code. Notice that it is identical to the test code in version #2. 3, Compile the Vehicle and Main classes locally. 4, Run the Main class. The output generated should be:
Creating a vehicle with a 10,000kg maximum load. Add box #1 (500kg) : true Add box #2 (250kg) : true Add box #3 (5000kg) : true Add box #4 (4000kg) : true Add box #5 (300kg) : false Vehicle load is 9750.0 kg
• You should see no change in the output of the program. This demonstrates that the (private) internal changes to the version #3 Vehicle class did not change the code of the client class Main.
Note:The test Main class is ready as is shown. you can add it to your code.
Preset Code
Prepend Code
/* 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,020kg maximum load.”);
Vehicle vehicle = new Vehicle(10020.0);
// Add a few boxes
System.out.println("Add box #1 (500kg) : " + vehicle.addBox(500.0));
System.out.println("Add box #2 (250kg) : " + vehicle.addBox(250.0));
System.out.println("Add box #3 (5000kg) : " + vehicle.addBox(5000.0));
System.out.println("Add box #4 (4000kg) : " + vehicle.addBox(4000.0));
System.out.println("Add box #5 (300kg) : " + vehicle.addBox(300.0));
System.out.println("Add box #5 (270kg) : " + vehicle.addBox(270.0));
// Print out the final vehicle load
System.out.println(“Vehicle load is " + vehicle.getLoad() + " kg”);
} }
/ PRESET CODE END - NEVER TOUCH CODE ABOVE /
代码:
import java.util.*;
class Vehicle {
private double load;
private double maxLoad;
public double getLoad()
{
return load;
}
public double getMaxLoad()
{
return maxLoad;
}
public Vehicle(double max_load)
{
maxLoad = max_load;
}
public boolean addBox(double weight)
{
if((load+weight)>getMaxLoad()) return false;
else
{
load += weight;
return true;
}
}
private double kiloToNewts(double weight)
{
return (weight * 9.8);
}
private double newtsToKilo(double weight)
{
return (weight / 9.8);
}
}
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,020kg maximum load.");
Vehicle vehicle = new Vehicle(10020.0);
// Add a few boxes
System.out.println("Add box #1 (500kg) : " + vehicle.addBox(500.0));
System.out.println("Add box #2 (250kg) : " + vehicle.addBox(250.0));
System.out.println("Add box #3 (5000kg) : " + vehicle.addBox(5000.0));
System.out.println("Add box #4 (4000kg) : " + vehicle.addBox(4000.0));
System.out.println("Add box #5 (300kg) : " + vehicle.addBox(300.0));
System.out.println("Add box #5 (270kg) : " + vehicle.addBox(270.0));
// Print out the final vehicle load
System.out.println("Vehicle load is " + vehicle.getLoad() + " kg");
} }
8.Reverse it
Given a number n, you are required to output its reverse counterpart. that is, the most significant digit become the list significant digit and so on. There are several test cases for each test group.
Input: Test case count T following by each test case.
Example:
5
12000
11111
012
5
-23
Output:
21
11111
21
5
-32
代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int times = s.nextInt();
for(int i = 0; i < times; i++)
{
int number = s.nextInt();
if(number < 0)
{
number = Math.abs(number);
System.out.print("-");
}
while(number%10 == 0) number /= 10;
while(number>=1)
{
System.out.print(number%10);
number /= 10;
}
System.out.print("\n");
}
s.close();
}
}