当前位置: 技术问答>java相关
java散分三题,来者有分,答对高分(限时)
来源: 互联网 发布时间:2015-02-10
本文导语: (1) Write a static main method for a class called PizzaPacker that determines how many pizzas can be packed into a box safely (i.e., lying flat ontop or beside one another without squishing). The program should prompt for three ints...
(1) Write a static main method for a class called PizzaPacker that
determines how many pizzas can be packed into a box safely (i.e., lying flat
ontop or beside one another without squishing). The program should prompt
for three ints (one at a time) representing the dimensions of the box. It
should also prompt for a float that represents the diameter of the pizzas
(all pizzas of the same diameter) and a float that represents the thickness
of the pizza. Then compute the number of whole pizzas that would fit in the
box safely. Note that you should ignore the weight of the pizzas and so you
can assume that a pizza at the bottom of a stack will not be squished.
Display the results in the Java console. Be sure to test your code with many
different boxes, pizza sizes and thicknesses. Assume that the pizzas are
only stacked on top of (or beside) each other neatly....no tipping pizzas
sideways :).
(2) The short type of Java holds an integer in the range of -32,768 to
32,767. Since in computers, data are stored in bits (binary digits), 16 bits
are required to store a variable of the short type. The number of possible
values that a short type variable can take is 216 = 65536, and these values
are in the range of -215 to (215-1). If we apply this backwards...given
the range of values (e.g. 0 to 65536), we can get the number of required
bits to store this number as log265536 = 16.
We now want to create our own data type called shorty that holds at least n
possible integer numbers, where n is not necessarily a "power of two". So,
for example, n may be 563846...meaning that a shorty can hold one of 563846
possible numbers. Write a Java program in
which the user enters the number n and then the program calculates and shows
the following:
The number of bits required by the shorty data type. This can be calculated
as the "ceiling" of log2n. Note that log2n can be obtained as bits = (log
n / log 2), where log is the natural logarithm. (Hint: see the Math class in
java).
The range of values (negative and positive) that the shorty can hold. This
range is calculated as being -2bits-1 to (2bits-1-1).
(3) Define an object called Customer which maintains the customer's name
(a string), phone number (a string), sex (a char), age (an integer), weight
(a float) and whether or not they are an adult (a boolean). Make accessing
and modifying methods. Create a main method in a separate class file which
creates a customer, sets its information and then retrieves and displays
this information to the console:
Customer bob = new Customer();
bob.setName("Bob");
bob.setPhone("555-5555");
bob.setSex('m');
bob.setAge(22);
bob.setWeight(165.67f);
bob.setAdult(true);
System.out.println("Name: " + bob.getName());
System.out.println("Phone: " + bob.getPhone());
System.out.println("Sex: " + bob.getSex());
System.out.println("Age: " + bob.getAge());
System.out.println("Weight: " + bob.getWeight());
System.out.println("Adult: " + bob.getAdult());
--------------------------------------------------------------------------------
determines how many pizzas can be packed into a box safely (i.e., lying flat
ontop or beside one another without squishing). The program should prompt
for three ints (one at a time) representing the dimensions of the box. It
should also prompt for a float that represents the diameter of the pizzas
(all pizzas of the same diameter) and a float that represents the thickness
of the pizza. Then compute the number of whole pizzas that would fit in the
box safely. Note that you should ignore the weight of the pizzas and so you
can assume that a pizza at the bottom of a stack will not be squished.
Display the results in the Java console. Be sure to test your code with many
different boxes, pizza sizes and thicknesses. Assume that the pizzas are
only stacked on top of (or beside) each other neatly....no tipping pizzas
sideways :).
(2) The short type of Java holds an integer in the range of -32,768 to
32,767. Since in computers, data are stored in bits (binary digits), 16 bits
are required to store a variable of the short type. The number of possible
values that a short type variable can take is 216 = 65536, and these values
are in the range of -215 to (215-1). If we apply this backwards...given
the range of values (e.g. 0 to 65536), we can get the number of required
bits to store this number as log265536 = 16.
We now want to create our own data type called shorty that holds at least n
possible integer numbers, where n is not necessarily a "power of two". So,
for example, n may be 563846...meaning that a shorty can hold one of 563846
possible numbers. Write a Java program in
which the user enters the number n and then the program calculates and shows
the following:
The number of bits required by the shorty data type. This can be calculated
as the "ceiling" of log2n. Note that log2n can be obtained as bits = (log
n / log 2), where log is the natural logarithm. (Hint: see the Math class in
java).
The range of values (negative and positive) that the shorty can hold. This
range is calculated as being -2bits-1 to (2bits-1-1).
(3) Define an object called Customer which maintains the customer's name
(a string), phone number (a string), sex (a char), age (an integer), weight
(a float) and whether or not they are an adult (a boolean). Make accessing
and modifying methods. Create a main method in a separate class file which
creates a customer, sets its information and then retrieves and displays
this information to the console:
Customer bob = new Customer();
bob.setName("Bob");
bob.setPhone("555-5555");
bob.setSex('m');
bob.setAge(22);
bob.setWeight(165.67f);
bob.setAdult(true);
System.out.println("Name: " + bob.getName());
System.out.println("Phone: " + bob.getPhone());
System.out.println("Sex: " + bob.getSex());
System.out.println("Age: " + bob.getAge());
System.out.println("Weight: " + bob.getWeight());
System.out.println("Adult: " + bob.getAdult());
--------------------------------------------------------------------------------
|
呵呵,快快的回答了最后一道题。
/**
* 淙氪死嗟乃得鱘n建立时间:(2001-9-28 16:04:41)
* 作者:
*/
public class Customer {
private java.lang.String name;
private java.lang.String phone;
private char sex;
private int age;
private float weight;
private boolean adult;
/**
* Customer constructor comment.
*/
public Customer() {
super();
}
/**
* n建立时间: (2001-9-28 16:06:07)
* 输入参数,输出参数,返回值:
* @return int
*/
public int getAge() {
return age;
}
/**
* 建立时间: (2001-9-28 16:05:11)
* 输入参数,输出参数,返回值:
* @return java.lang.String
*/
public java.lang.String getName() {
return name;
}
/**
* 建立时间: (2001-9-28 16:05:40)
* 输入参数,输出参数,返回值:
* @return java.lang.String
*/
public java.lang.String getPhone() {
return phone;
}
/**
* 建立时间: (2001-9-28 16:05:56)
* 输入参数,输出参数,返回值:
* @return char
*/
public char getSex() {
return sex;
}
/**
* 建立时间: (2001-9-28 16:06:22)
* 输入参数,输出参数,返回值:
* @return float
*/
public float getWeight() {
return weight;
}
/**
* 建立时间: (2001-9-28 16:06:41)
* 输入参数,输出参数,返回值:
* @return boolean
*/
public boolean isAdult() {
return adult;
}
/**
* 建立时间: (2001-9-28 16:06:41)
* 输入参数,输出参数,返回值:
* @param newAdult boolean
*/
public void setAdult(boolean newAdult) {
adult = newAdult;
}
/**
* 建立时间: (2001-9-28 16:06:07)
* 输入参数,输出参数,返回值:
* @param newAge int
*/
public void setAge(int newAge) {
age = newAge;
}
/**
* 建立时间: (2001-9-28 16:05:11)
* 输入参数,输出参数,返回值:
* @param newName java.lang.String
*/
public void setName(java.lang.String newName) {
name = newName;
}
/**
* 建立时间: (2001-9-28 16:05:40)
* 输入参数,输出参数,返回值:
* @param newPhone java.lang.String
*/
public void setPhone(java.lang.String newPhone) {
phone = newPhone;
}
/**
* 建立时间: (2001-9-28 16:05:56)
* 输入参数,输出参数,返回值:
* @param newSex char
*/
public void setSex(char newSex) {
sex = newSex;
}
/**
* 建立时间: (2001-9-28 16:06:22)
* 输入参数,输出参数,返回值:
* @param newWeight float
*/
public void setWeight(float newWeight) {
weight = newWeight;
}
}
/**
* 淙氪死嗟乃得鱘n建立时间:(2001-9-28 16:04:41)
* 作者:
*/
public class Customer {
private java.lang.String name;
private java.lang.String phone;
private char sex;
private int age;
private float weight;
private boolean adult;
/**
* Customer constructor comment.
*/
public Customer() {
super();
}
/**
* n建立时间: (2001-9-28 16:06:07)
* 输入参数,输出参数,返回值:
* @return int
*/
public int getAge() {
return age;
}
/**
* 建立时间: (2001-9-28 16:05:11)
* 输入参数,输出参数,返回值:
* @return java.lang.String
*/
public java.lang.String getName() {
return name;
}
/**
* 建立时间: (2001-9-28 16:05:40)
* 输入参数,输出参数,返回值:
* @return java.lang.String
*/
public java.lang.String getPhone() {
return phone;
}
/**
* 建立时间: (2001-9-28 16:05:56)
* 输入参数,输出参数,返回值:
* @return char
*/
public char getSex() {
return sex;
}
/**
* 建立时间: (2001-9-28 16:06:22)
* 输入参数,输出参数,返回值:
* @return float
*/
public float getWeight() {
return weight;
}
/**
* 建立时间: (2001-9-28 16:06:41)
* 输入参数,输出参数,返回值:
* @return boolean
*/
public boolean isAdult() {
return adult;
}
/**
* 建立时间: (2001-9-28 16:06:41)
* 输入参数,输出参数,返回值:
* @param newAdult boolean
*/
public void setAdult(boolean newAdult) {
adult = newAdult;
}
/**
* 建立时间: (2001-9-28 16:06:07)
* 输入参数,输出参数,返回值:
* @param newAge int
*/
public void setAge(int newAge) {
age = newAge;
}
/**
* 建立时间: (2001-9-28 16:05:11)
* 输入参数,输出参数,返回值:
* @param newName java.lang.String
*/
public void setName(java.lang.String newName) {
name = newName;
}
/**
* 建立时间: (2001-9-28 16:05:40)
* 输入参数,输出参数,返回值:
* @param newPhone java.lang.String
*/
public void setPhone(java.lang.String newPhone) {
phone = newPhone;
}
/**
* 建立时间: (2001-9-28 16:05:56)
* 输入参数,输出参数,返回值:
* @param newSex char
*/
public void setSex(char newSex) {
sex = newSex;
}
/**
* 建立时间: (2001-9-28 16:06:22)
* 输入参数,输出参数,返回值:
* @param newWeight float
*/
public void setWeight(float newWeight) {
weight = newWeight;
}
}
|
先做个简单的:)
public class Customer {
private String name;
private String phone;
private char sex;
private int age;
private float weight;
private boolean adult;
public Customer() {
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setPhone(String phone){
this.phone=phone;
}
public String getPhone(){
return this.phone;
}
public void setSex(char sex){
this.sex=sex;
}
public char getSex(){
return this.sex;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setWeight(float weight){
this.weight=weight;
}
public float getWeight(){
return this.weight;
}
public void setAdult(boolean adult){
this.adult=adult;
}
public boolean getAdult(){
return this.adult;
}
public static void main(String[] args) {
Customer customer = new Customer();
customer.setName("Bob");
customer.setPhone("555-5555");
customer.setSex('m');
customer.setAge(22);
customer.setWeight(165.67f);
customer.setAdult(true);
System.out.println("Name: " + customer.getName());
System.out.println("Phone: " + customer.getPhone());
System.out.println("Sex: " + customer.getSex());
System.out.println("Age: " + customer.getAge());
System.out.println("Weight: " + customer.getWeight());
System.out.println("Adult: " + customer.getAdult());
}
}
public class Customer {
private String name;
private String phone;
private char sex;
private int age;
private float weight;
private boolean adult;
public Customer() {
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setPhone(String phone){
this.phone=phone;
}
public String getPhone(){
return this.phone;
}
public void setSex(char sex){
this.sex=sex;
}
public char getSex(){
return this.sex;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setWeight(float weight){
this.weight=weight;
}
public float getWeight(){
return this.weight;
}
public void setAdult(boolean adult){
this.adult=adult;
}
public boolean getAdult(){
return this.adult;
}
public static void main(String[] args) {
Customer customer = new Customer();
customer.setName("Bob");
customer.setPhone("555-5555");
customer.setSex('m');
customer.setAge(22);
customer.setWeight(165.67f);
customer.setAdult(true);
System.out.println("Name: " + customer.getName());
System.out.println("Phone: " + customer.getPhone());
System.out.println("Sex: " + customer.getSex());
System.out.println("Age: " + customer.getAge());
System.out.println("Weight: " + customer.getWeight());
System.out.println("Adult: " + customer.getAdult());
}
}
|
第二道题的答案,只不过是英文的,其实简单的狠。
/**
* 此类的描述
* 建立时间:(2001-9-28 16:16:46)
* 作者:
*/
public class Shorty {
private long number;
private int bits;
/**
* Shorty constructor comment.
*/
public Shorty(long n) {
super();
this.number = n;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:17:55)
* 输入参数,输出参数,返回值:
*/
public int getBits() {
bits = (int) Math.ceil(Math.log(number) / Math.log(2));
return bits;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:17:30)
* 输入参数,输出参数,返回值:
* @return long
*/
public long getNumber() {
return number;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:20:00)
* 输入参数,输出参数,返回值:
* @return java.lang.String
*/
public String getRange() {
String returnValue =
"The Range is From : -"
+ (int) Math.pow(2, bits - 1)
+ " To "
+ ((int) Math.pow(2, bits - 1) - 1);
return returnValue;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:22:28)
* 输入参数,输出参数,返回值:
* @param args java.lang.String[]
*/
public static void main(String[] args) {
Shorty test = new Shorty(65534);
System.out.println(test.getBits());
System.out.println(test.getRange());
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:17:30)
* 输入参数,输出参数,返回值:
* @param newNumber long
*/
public void setNumber(long newNumber) {
number = newNumber;
}
}
/**
* 此类的描述
* 建立时间:(2001-9-28 16:16:46)
* 作者:
*/
public class Shorty {
private long number;
private int bits;
/**
* Shorty constructor comment.
*/
public Shorty(long n) {
super();
this.number = n;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:17:55)
* 输入参数,输出参数,返回值:
*/
public int getBits() {
bits = (int) Math.ceil(Math.log(number) / Math.log(2));
return bits;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:17:30)
* 输入参数,输出参数,返回值:
* @return long
*/
public long getNumber() {
return number;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:20:00)
* 输入参数,输出参数,返回值:
* @return java.lang.String
*/
public String getRange() {
String returnValue =
"The Range is From : -"
+ (int) Math.pow(2, bits - 1)
+ " To "
+ ((int) Math.pow(2, bits - 1) - 1);
return returnValue;
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:22:28)
* 输入参数,输出参数,返回值:
* @param args java.lang.String[]
*/
public static void main(String[] args) {
Shorty test = new Shorty(65534);
System.out.println(test.getBits());
System.out.println(test.getRange());
}
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:17:30)
* 输入参数,输出参数,返回值:
* @param newNumber long
*/
public void setNumber(long newNumber) {
number = newNumber;
}
}
|
第一道题的,老兄。
/**
* 此类的描述
* 建立时间:(2001-9-28 16:33:04)
* 作者:宓吉
*/
public class PizzaPacker {
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:33:09)
* 输入参数,输出参数,返回值:
* @param args java.lang.String[]
*/
public static void main(String[] args) {
int boxHeight = 100;
int boxAxisX = 100;
int boxAxisY = 100;
float pizzaThick = 15;
float pizzaDiameter = 70;
int smallHeight = (int) Math.floor(boxHeight / pizzaThick);
int smallX = (int) Math.floor(boxAxisX / pizzaDiameter);
int smallY = (int) Math.floor(boxAxisY / pizzaDiameter);
int answer = smallHeight * smallX * smallY;
System.out.println(answer);
}
}
/**
* 此类的描述
* 建立时间:(2001-9-28 16:33:04)
* 作者:宓吉
*/
public class PizzaPacker {
/**
* 此方法的描述
* 建立时间: (2001-9-28 16:33:09)
* 输入参数,输出参数,返回值:
* @param args java.lang.String[]
*/
public static void main(String[] args) {
int boxHeight = 100;
int boxAxisX = 100;
int boxAxisY = 100;
float pizzaThick = 15;
float pizzaDiameter = 70;
int smallHeight = (int) Math.floor(boxHeight / pizzaThick);
int smallX = (int) Math.floor(boxAxisX / pizzaDiameter);
int smallY = (int) Math.floor(boxAxisY / pizzaDiameter);
int answer = smallHeight * smallX * smallY;
System.out.println(answer);
}
}
|
gz
|
up
|
没有中文呀,我英语很差,看不明白
|
??
|
看不懂鸟语啊`
|
高手!