Download CS 101 Quiz No:4 Section:03

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 101 Quiz No:4 Section:03
Problem a: Write a Java application that uses the Integer wrapper class to find the binary representation of
an input integer number (use toBinaryString (anInt) method of the Integer wrapper class) as long as the
user wants to input more integer numbers, and the number of input integers that are negative.
Possible interaction with the user:
Please type an integer number:5
The corresponding binary representation of 5 is 101
Do you want to convert another decimal number into a binary number (Yes/No):yes
Please type an integer number:0
The corresponding binary representation of 0 is 0
Do you want to convert another decimal number into a binary number (Yes/No):yes
Please type an integer number:-5
The corresponding binary representation of -5 is 11111111111111111111111111111011
Do you want to convert another decimal number into a binary number (Yes/No):no
You have input 1 negative numbers.
Problem b: Suppose we want to add a new property for the name of a pet shop that owns the pet objects to
the pet class of 3rd quiz. Write the necessary variable declaration, getter and setter methods for this property
in pet class. Also write the necessary statement for a client class to set this property to “Cute Pets”.
Solution for part a:
import java.util.Scanner;
public class q4a {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in) ;
int x, nn=0;
String answer, binary;
do {
System.out.print ("Please type an integer number:");
x=keyboard.nextInt();
if (x<0)
nn++;
binary = Integer.toBinaryString (x);
System.out.print ("The corresponding binary representation of ");
System.out.println (x + " is " + binary);
System.out.print
("Do you want to convert another decimal number into a binary number (Yes/No):");
answer = keyboard.next();
} while (answer.equalsIgnoreCase("Yes") );
System.out.println ("You have input " + nn + " negative numbers.");
}
}
Solution for part b:
import java.util.Random;
public class petQ4 {
private String petsName;
private int petsAge;
private char gender;
private static String petshopName="";
public petQ4() {
petsName = "" ;
int n = randomInteger(3,8);
for (int i=0;i<n;i++){
int order = randomInteger(65,90);
petsName += (char) order;
}
petsAge = randomInteger(0,12);
gender = 'M' ;
if (randomInteger (0,1) == 1)
gender = 'F';
}
public petQ4 (String name, int age, char gender){
petsName = name ;
petsAge = age ;
this.gender = gender ;
}
public static void setPetshopName (String s) {
petshopName = s ;
}
public static String getPetshopName () {
return petshopName;
}
public String getName (){
return petsName;
}
public int getAge (){
return petsAge;
}
public char getGender (){
return gender;
}
public String toString () {
String S = petsName + ", " + petsAge ;
S += " years old and " ;
if (gender == 'F')
S += "female." ;
else
S += "male.";
return S;
}
public int compareTo (petQ4 other) {
int x = this.petsAge - other.petsAge;
return x;
}
private int randomInteger (int a, int b){
Random rnd = new Random ();
return (a + rnd.nextInt (b-a+1) );
}
}
public class q4s3pb {
public static void main (String [] args) {
int i, age, n = 0;
petQ4 randomPet;
char initial;
petQ4.setPetshopName ("Cute Pets");
System.out.println("Random 100 pets for the petshop, "+petQ4.getPetshopName());
for (i=0;i<100;i++){
randomPet = new petQ4 ();
System.out.println (randomPet);
initial = randomPet.getName().charAt(0);
age = randomPet.getAge();
if (initial == 'D' && age>5)
n++;
}
System.out.println
("The number of pets with name starting with D and older than 5 is " + n);
}
}