In this exercise you will create two subclasses of the Account class in the Banking project: SavingsAccount and CheckingAccount. You will override the withdraw method for checking accounts and use super to invoke a parent constructor.
Note: you must have completed the previous exercises of the banking project before this one.
Start by changing your working directory to exercise1 on your computer(Or anywhere else you like).
Implementing the Account Subclasses
To the banking package, you will add the SavingsAccount and CheckingAccount subclasses as modeled by the UML diagram above.
Modify the Account Class
The Savings Account Subclass
The Checking Account Subclass
Test the Code:
In the main exercise1 directory, compile and execute the Main program. The output should be:
Creating the customer Jane Smith. Creating her Savings Account with a 500.00 balance and 3% interest. Creating the customer Owen Bryant. Creating his Checking Account with a 500.00 balance and no overdraft protection. Creating the customer Tim Soley. Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection. Creating the customer Maria Soley. Maria shares her Checking Account with her husband Tim.
Retrieving the customer Jane Smith with her savings account. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: false Customer [Simms, Jane] has a balance of 324.88
Retrieving the customer Owen Bryant with his checking account with no overdraft protection. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: false Customer [Bryant, Owen] has a balance of 324.88
Retrieving the customer Tim Soley with his checking account that has overdraft protection. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: true Customer [Soley, Tim] has a balance of 0.0
Retrieving the customer Maria Soley with her joint checking account with husband Tim. Deposit 150.00: true Withdraw 750.00: false Customer [Soley, Maria] has a balance of 150.0
Notice that Jane's savings account and Owen's checking account fundamentaly behave as a plain-old bank account. But Tim & Maria's joint checking account has 500.00 worth of overdraft protection. Tim's transactions dip into that protection and therefore his ending balance is 0.00. His account's overdraft protection level is 424.88. Finally, Maria deposits 150.00 into this joint account; raising the balance from 0.00 to 150.00. Then she tries withdraw 1000.00, which fails because neither the balance nor the overdraft protection can cover that requested amount.
前置代码
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
//import banking.*;
public class Main{
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account;
//
// Create bank customers and their accounts
//
System.out.println("Creating the customer Jane Smith.");
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
customer.setAccount(new SavingsAccount(500.00, 0.03));
System.out.println("Creating the customer Owen Bryant.");
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
customer.setAccount(new CheckingAccount(500.00));
System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
customer.setAccount(new CheckingAccount(500.00, 500.00));
System.out.println("Creating the customer Maria Soley.");
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
System.out.println("Maria shares her Checking Account with her husband Tim.");
customer.setAccount(bank.getCustomer(2).getAccount());
System.out.println();
//
// Demonstrate behavior of various account types
//
// Test a standard Savings Account
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomer(0);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
System.out.println();
// Test a Checking Account w/o overdraft protection
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer = bank.getCustomer(1);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
System.out.println();
// Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer = bank.getCustomer(2);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
System.out.println();
// Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
customer = bank.getCustomer(3);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Deposit 150.00: " + account.deposit(150.00));
System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
}
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
问题要求比较简单,就是学会用extends的用法编写class的继承即可
class Account {
protected double balance;
public Account(double init_balance) {
balance = init_balance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amt) {
balance += amt;
return true;
}
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else
return false;
}
}
class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(double balance, double interest_rate) {
super(balance);
interestRate = interest_rate;
this.balance = balance;
}
}
class CheckingAccount extends Account {
private double overdraftProtection;
public CheckingAccount(double init_balance) {
super(init_balance);
}
public CheckingAccount(double init_balance, double protect) {
super(init_balance);
this.overdraftProtection = protect;
}
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
}
else {
if (overdraftProtection >= (amt - balance)) {
overdraftProtection -= (amt - balance);
balance = 0;
}
else {
return false;
}
}
return true;
}
}
class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f, String l) {
firstName = f;
lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account acct) {
account = acct;
}
}
class Bank {
private Customer[] customers = new Customer[15];
private int numberOfCustomers;
public Bank() {
}
public void addCustomer(String f, String l) {
customers[numberOfCustomers] = new Customer(f, l);
numberOfCustomers++;
}
public int getNumOfCustomers() {
return numberOfCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
}
In this exercise you will create an heterogeneous array to represent the aggregation of customers to accounts. That is, a given customer may have several accounts of different types.
Start by changing your working directory to exercise2 on your computer(or anywhere else you like).
Modify the Customer Class
Complete the Main Program
This program creates a set of customers and accounts and generates a report of customers and their account balances.
In the Main.java file you will find comment blocks that start and end with /*** ... ***/. These comments indicate the location in the code that you must supply. The content of the file is as follows:
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
//import banking.*;
import java.text.NumberFormat;
public class Main{
public static void main(String[] args) {
Bank bank = new Bank();
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Customer customer;
int curCustomer = 0;
Scanner s = new Scanner(System.in);
int t = s.nextInt();
s.nextLine();
// Create several customers and their accounts according to data
while (t-- > 0) {
String f = s.next();
String l = s.next();
s.nextLine();
bank.addCustomer(f, l);
customer = bank.getCustomer(curCustomer++);
int numAccount = s.nextInt();
s.nextLine();
while (numAccount-- > 0) {
String[] type = s.nextLine().split(" ");
double balance;
double interesOrProtect;
if (type[0] == "C" || type[0] == "c") {
balance = Double.parseDouble(type[1]);
if (type.length == 3) {
interesOrProtect = Double.parseDouble(type[2]);
customer.addAccount(new CheckingAccount(balance,
interesOrProtect));
} else {
customer.addAccount(new CheckingAccount(balance));
interesOrProtect = Double.parseDouble(type[2]);
}
} else if (type[0] == "S" || type[0] == "s") {
balance = Double.parseDouble(type[1]);
interesOrProtect = Double.parseDouble(type[2]);
customer.addAccount(new SavingsAccount(balance,
interesOrProtect));
} else if (type[0] == "A" || type[0] == "a") {
int cIndex = Integer.parseInt(type[1]);
int aIndex = Integer.parseInt(type[1]);
customer.addAccount(bank.getCustomer(cIndex).getAccount(
aIndex));
}
}
}
// Generate a report
System.out.println("CUSTOMERS REPORT");
System.out.println("================");
for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
}
}
}
}
CUSTOMERS REPORT ================ Customer: Simms, Jane Savings Account$500.00 Checking Account$200.00 Customer: Bryant, Owen Checking Account$200.00 Customer: Soley, Tim Savings Account$1,500.00 Checking Account$200.00 Customer: Soley, Maria Checking Account$200.00
调用之前写的class,在这里是补全代码,按照需要的格式输出,可以学习使用Java有的Number Format用法,根据地区格式化输出货币,我们把地区赋值为US即可
import java.util.*;
import java.text.NumberFormat;
public class Main{
public static void main(String[] args) {
Bank bank = new Bank();
NumberFormat currency_format = NumberFormat.getCurrencyInstance(Locale.US);
Customer customer;
int curCustomer = 0;
Scanner s = new Scanner(System.in);
int t = s.nextInt();
s.nextLine();
while (t-- > 0) {
String f = s.next();
String l = s.next();
s.nextLine();
bank.addCustomer(f, l);
customer = bank.getCustomer(curCustomer++);
int numAccount = s.nextInt();
s.nextLine();
while (numAccount-- > 0) {
String[] type = s.nextLine().split(" ");
double balance;
double interesOrProtect;
if (Objects.equals(type[0], "C") || Objects.equals(type[0], "c")) {
balance = Double.parseDouble(type[1]);
if (type.length == 3) {
interesOrProtect = Double.parseDouble(type[2]);
customer.addAccount(new CheckingAccount(balance,
interesOrProtect));
} else {
customer.addAccount(new CheckingAccount(balance));
}
} else if (Objects.equals(type[0], "S") || Objects.equals(type[0], "s")) {
balance = Double.parseDouble(type[1]);
interesOrProtect = Double.parseDouble(type[2]);
customer.addAccount(new SavingsAccount(balance,
interesOrProtect));
} else if (Objects.equals(type[0], "A") || Objects.equals(type[0], "a")) {
int cIndex = Integer.parseInt(type[2]);
int aIndex = Integer.parseInt(type[1]);
customer.addAccount(bank.getCustomer(cIndex).getAccount(
aIndex));
}
}
}
System.out.println("CUSTOMERS REPORT");
System.out.println("================");
for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
if (account instanceof SavingsAccount) {
account_type = " Savings Account";
} else if (account instanceof CheckingAccount) {
account_type = " Checking Account";
}
System.out.println(account_type + currency_format.format(account.getBalance()));
}
}
}
}
class Account {
protected double balance;
public Account(double init_balance) {
balance = init_balance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amt) {
balance += amt;
return true;
}
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else
return false;
}
}
class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(double balance, double interest_rate) {
super(balance);
interestRate = interest_rate;
this.balance = balance;
}
}
class CheckingAccount extends Account {
private double overdraftProtection;
public CheckingAccount(double init_balance) {
super(init_balance);
}
public CheckingAccount(double init_balance, double protect) {
super(init_balance);
this.overdraftProtection = protect;
}
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
} else {
if (overdraftProtection >= (amt - balance)) {
overdraftProtection -= (amt - balance);
balance = 0;
}
else {
return false;
}
}
return true;
}
}
class Customer {
private String firstName;
private String lastName;
private Account[] accounts;
private int numberOfAccounts;
public Customer(String f, String l) {
firstName = f;
lastName = l;
accounts = new Account[2];
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount(int index) {
return accounts[index];
}
public int getNumOfAccounts() {
return numberOfAccounts;
}
public void addAccount(Account account) {
accounts[numberOfAccounts++] = account;
}
}
class Bank {
private Customer[] customers = new Customer[15];
private int numberOfCustomers;
public Bank() {
}
public void addCustomer(String f, String l) {
customers[numberOfCustomers] = new Customer(f, l);
numberOfCustomers++;
}
public int getNumOfCustomers() {
return numberOfCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
}
In this exercise you will create two subclasses of the Account class in the banking project: SavingsAccount and CheckingAccount.
This is an alternate version of Another Exercise. It incorporate a more complex model of the overdraft protection mechanism. It uses the customer's savings account to perform the overdraft protection.
Start by changing your working directory to alternate1 on your computer(or anywahere else you like).
Implementing the Account Subclasses
To the banking package, you will add the SavingsAccount and CheckingAccount subclasses as modeled by the UML diagram above.
Modifying Customer to Hold Two Accounts
Modify the Customer class to hold two bank accounts: one for savings and one for checking; both are optional.
Test the Code:
In the main alternate1 directory, compile and execute the Main program. The output should look similar to these:
[Jane, Simms] create savings account: 1500.0 [Jane, Simms] create Checking account: 300.0 with a protecting savings accout: 1500.0 ...... Checking acct [Jane, Simms]withdraw 1250.0 succeeds? true Checking acct [Owen, Bryant]deposit 22.5 succeeds? true ...... Checking acct [Jane, Simms]deposit 423.0 succeeds? true Checking acct [Owen, Bryant]withdraw 553.23 succeeds? false
Customer: Jane, Simms has a checking balance of 698.0 has a savings balance of 102.38
......
Customer: Maggie, White has a checking balance of 1103.0 has a savings balance of 300.0
Notice that Jane's checking account is protected by her savings account in the last transaction; whereas, Owen has no overdraft protection, so the last transaction on his account fails and the balance is not affected.
class Account {
protected double balance;
public Account(double init_balance) {
balance = init_balance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amt) {
balance += amt;
return true;
}
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else
return false;
}
}
class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(double balance, double interest_rate) {
super(balance);
interestRate = interest_rate;
this.balance = balance;
}
}
class CheckingAccount extends Account {
private Account protectedBy = null;
public CheckingAccount(double init_balance) {
super(init_balance);
}
public CheckingAccount(double init_balance, Account protect) {
super(init_balance);
this.protectedBy = protect;
}
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
} else {
if (protectedBy != null && protectedBy.getBalance() >= (amt - balance)) {
protectedBy.withdraw(amt - balance);
balance = 0;
} else {
return false;
}
}
return true;
}
}
class Bank {
private Customer[] customers = new Customer[15];
private int numberOfCustomers;
public Bank() {
}
public void addCustomer(String f, String l) {
customers[numberOfCustomers] = new Customer(f, l);
numberOfCustomers++;
}
public int getNumOfCustomers() {
return numberOfCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
}
class Customer {
private String firstName;
private String lastName;
private Account[] accounts;
private int numberOfAccounts;
private Account savingsAccount = null;
private Account checkingAccount = null;
public Customer(String f, String l) {
firstName = f.toString();
lastName = l.toString();
accounts = new Account[2];
}
public String toString() {
return "[" + lastName + ", " + firstName + "]";
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount(int index) {
return accounts[index];
}
public int getNumOfAccounts() {
return numberOfAccounts;
}
public void addAccount(Account account) {
accounts[numberOfAccounts++] = account;
}
public Account getSavings() {
return savingsAccount;
}
public void setSavings(Account savingsAccount) {
this.savingsAccount = savingsAccount;
}
public Account getChecking() {
return checkingAccount;
}
public void setChecking(Account checkingAccount) {
this.checkingAccount = checkingAccount;
}
}
An ellipse is a figure on a plane where the sum of the distances from any point on its perimeter to two fixed points is constant. The two fixed points are called foci (plural of focus). In this problem we are interested in the number of points with integral coordinates that lie strictly inside of the given ellipse.
The foci are (x1, y1) and (x2, y2), and the fixed sum of distances is d.
Constraints
x1 , y1, x2, y2 will be between -100 and 100, inclusive.
d will be between 1 and 200, inclusive.
The arguments will define a valid ellipse with positive area.
The first line contains a single integer T tells that there are T cases in the problem. Then for each case, there are 5 integers, x1, y1, x2, y2 and d in order.
The number of points with integral coordinates that lie strictly inside of the given ellipse.
2
0 0 0 0 4
-3 0 3 0 10
9
59
求椭圆覆盖的整数位置的点,先求取椭圆中心点,然后再d/2范围内查找,离两个焦点的距离和小于d
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while (n-->0) {
int x1 = input.nextInt();
int y1 = input.nextInt();
int x2 = input.nextInt();
int y2 = input.nextInt();
int d = input.nextInt();
int x3 = (x1+x2)/2;
int y3 = (y1+y2)/2;
int count = 0;
for(int x = x3-(d/2);x <= x3+(d/2) ;x++){
for(int y = y3-(d/2);y <= y3+(d/2);y++){
if(Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))+Math.sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2)) < d) count++;
}
}
System.out.println(count);
}input.close();
}
}
Thimbles is a hazard game with the following rules. The dealer puts three identical thimbles on the table, with a small ball underneath the first one. Then, he repeatedly selects a pair of thimbles and swaps their positions. Finally, he asks you "Where is the ball?". You win if you point to the right thimble and lose otherwise.
You are writing the computer version of this game, and in this problem, you are to write a program that determines the position of the ball after all the thimble swaps have been done.
You will be given a series of swaps which describes the swaps made, in order. Each element of swaps will be in the format "X-Y" (quotes for clarity), which means that the thimble in position X was swapped with the thimble in position Y. The positions are 1, 2 or 3. Your method should return the position of the thimble with the ball after all the swaps.
swaps will contain between 1 and 50 elements, inclusive.
Each element of swaps will be in the format "X-Y" (quotes for clarity) where X and Y are distinct digits between 1 and 3, inclusive.
The first line contains a single integer T tells that there are T case in the problem. Then for each case, there is a line tell you all the operations. The first integer of the line is the number of swap operations, following a series of X-Y swap operations.
the position of the ball after all the thimble swaps have been done.
2
2 1-2 3-1
4 3-1 2-3 3-1 3-2
2
3
求顶针的位置,初始位置在1,求多次交换后位置,可以用一个值单独存储此刻顶针位置,即可实现问题求解
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i = 0; i < n; i++){
int step = s.nextInt();
char now = '1';
for(int j = 0; j < step; j++){
String str = s.next();
char p1 = str.charAt(0);
char p2 = str.charAt(2);
if(now == p1) now = p2;
else if(now == p2) now = p1;
}
System.out.println(now);
}
s.close();
}
}
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.
The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:
A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.
Generate a line of output for those telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order.
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
310-1010 2
487-3279 4
888-4567 3
参考了别人的实现方法,可以利用map解决问题
SMS messages are short messages sent between mobile phones. The maximum length of a single message is 160 characters, so it is often necessary to abbreviate words.
You are given a String text, and your task is to translate it to SMS language according to the following rules:
All quotes are for clarity only. The rules must be applied in the order they appear in the list. For example, "I HATE rats, and you?" will be translated to "i h8 r@s & U".
Hint: you are encouraged to use StringBuffer class for String manipunations. Of course, you can also use other methods you like.
The first line contains a single integer T tells that there are T case in the problem. Then there are T lines of strings.
The resulting translation as a String.
2
I HATE rats, and you?
What is the weather like today?
i h8 r@s & U
wh@ is the we@her like today
按照题目要求,一步一步对字符串进行操作即可,nextLine识别一整行进入字符串再利用Java的replace用法进行字符串变换
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.nextLine();
for(int i =0; i < n; i++) {
String str = s.nextLine();
String a =str.replaceAll("[\\pP‘’“”]","");
a = a.toLowerCase();
a = a.replace("and", "&");
a = a.replace("ate", "8");
a = a.replace("at", "@");
a = a.replace("at", "@");
a = a.replace("you","U");
System.out.println(a);
}
}
}