1. Write an application that creates and prints a random identification number i
ID: 3562613 • Letter: 1
Question
1.
Write an application that creates and prints a random identification number in the form XXX-XX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 and make sure the second set of digits is not greater than 74. Hint: Think through the easiest way to construct the identification number. Each digit does not have to be determined separately.
Remember to include the comment block required at the beginning of all Java programs. It should resemble
/*
* Author: Date:
* Filename: COP 2551 - Project 2b
* Purpose:
*/
When everything is checked and running perfectly, copy the output from the Console into a comment block after the end of your program. This shows that your program was running and produced the correct output.
2.
Design and implement a class called Puppy that contains instance data representing a puppy
Explanation / Answer
/*
* Author: Raks
* Date: 20-Sep-2014
* Filename: RandomIdNum.java
* Purpose: generate random number XXX-XX-XXXXX
*/
import java.util.Random;
public class RandomIdNum {
public static void main(String Args[])
{
String randstr="";
Random rand = new Random(System.currentTimeMillis());
int i = Math.abs(rand.nextInt()%74);
randstr= String.valueOf(Math.abs(rand.nextInt()%8)) + String.valueOf(Math.abs(rand.nextInt()%8)) + String.valueOf(Math.abs(rand.nextInt()%8)) + "-" +
((i<10)?"0"+String.valueOf(i):String.valueOf(i)) + "-" +
String.valueOf(Math.abs(rand.nextInt()*9999)).substring(0, 5);
System.out.println(randstr);
}
}
/*
* 035-41-72081
*/
package app_chefarr;
/*
* Author: Raks
* Date: 20-Sep-2014
* Filename: RandomIdNum.java
* Purpose: generate random number XXX-XX-XXXXX
*/
public class PetStore {
public static class Puppy
{
private String breed,color;
private float weight;
public Puppy()
{
breed="German Shephard";
color="Brown";
weight=5;
}
public Puppy(String breed,String color,float weight)
{
this.breed=breed;
this.color=color;
this.weight=weight;
}
public String getBreed() {return breed;}
public String getColor() {return color;}
public float getWeight() {return weight;}
public void setBreed(String value) {breed=value;}
public void setColor(String value) {color=value;}
public void setWeight(float value) {weight=value;}
public float getFood(){return weight*Float.parseFloat("0.74");}
public String toString()
{
return "Breed: "+ breed +",Color: "+ color +",Weight: "+ weight + ",Food: "+ getFood();
}
}
public static void main(String Args[])
{
Puppy p1=new Puppy();
Puppy p2=new Puppy();
Puppy p3=new Puppy();
Puppy p4=new Puppy();
Puppy p5=new Puppy();
Puppy p6=new Puppy();
System.out.println("Puppies for sale:");
System.out.println(p1.toString());
System.out.println(p2.toString());
System.out.println(p3.toString());
System.out.println(p4.toString());
System.out.println(p5.toString());
System.out.println(p6.toString());
p1.setBreed("Pomerian");
p2.setBreed("Poodle");
p3.setColor("White");
p4.setColor("Black");
p5.setWeight(4);
p6.setWeight(7);
System.out.println("Changed Data: " + p1.getBreed() + ","+ p2.getBreed()+","+p3.getColor()+","+p4.getColor()+","+p5.getWeight()+","+p6.getWeight());
System.out.println("Puppies for sale:");
System.out.println(p1.toString());
System.out.println(p2.toString());
System.out.println(p3.toString());
System.out.println(p4.toString());
System.out.println(p5.toString());
System.out.println(p6.toString());
}
}
/*
* Puppies for sale:
Breed: German Shephard,Color: Brown,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Brown,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Brown,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Brown,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Brown,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Brown,Weight: 5.0,Food: 3.7
Changed Data: Pomerian,Poodle,White,Black,4.0,7.0
Puppies for sale:
Breed: Pomerian,Color: Brown,Weight: 5.0,Food: 3.7
Breed: Poodle,Color: Brown,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: White,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Black,Weight: 5.0,Food: 3.7
Breed: German Shephard,Color: Brown,Weight: 4.0,Food: 2.96
Breed: German Shephard,Color: Brown,Weight: 7.0,Food: 5.1800003
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.