In the following method, why the factorial goes wrong when n>12 ? */
static int factorial(int n){
int fac=1;
if ((n<0) || (n>12))
return -1;
for (int i=2; i<=n;i++)
fac *=i;
return fac;
}
In the following method, why the factorial goes wrong when n>12 ? */
static int factorial(int n){
int fac=1;
if ((n<0) || (n>12))
return -1;
for (int i=2; i<=n;i++)
fac *=i;
return fac;
}
/* Operation on DOS window: "PositiveInt1" receives a positive integer only. */
import java.util.*;
public class PositiveInt1{
public static void main(String args[]){
int n=0;
Scanner in = new Scanner(System.in);
while(true){
try{
System.out.println("Input a non-negative integer:");
n = in.nextInt();
}catch(InputMismatchException e){
System.out.println("Your input is mismatched.");
in.next();
continue;
}
if (n<0){
System.out.println("You negative value is not valid.");
continue;
}
break;
}
System.out.printf("Your input is %d\n", n);
}
}
/* “PositiveInt.java” receives an integer greater than 0. In this program Exceptions are catched accordingly */
import javax.swing.*;
public class PositiveInt{
static class NonNegativeException extends Exception{
public NonNegativeException(String msg){
super(msg);
}
}
static void show(int x)throws NonNegativeException {
if (x<0)
throw new NonNegativeException("Negative integer is not valid.");
}
public static void main(String args[]){
int n=0;
while(true){
try{
String s=JOptionPane.showInputDialog(null,"Input a positive integer:");
n = Integer.parseInt(s);
show(n);
}catch(NumberFormatException e){
System.out.println();
JOptionPane.showMessageDialog( null, "Your input is mismatched. Try again.", "Warning ",JOptionPane.WARNING_MESSAGE);
continue;
}catch(NonNegativeException e){
JOptionPane.showMessageDialog( null, e.getMessage(), "Warning ",JOptionPane.WARNING_MESSAGE);
continue;
}
break;
}
JOptionPane.showMessageDialog(null,"Your input: "+n,"Welcome to DANIWEB", JOptionPane.INFORMATION_MESSAGE);
}
}
/*The rocks are classified into the following 3 groups in origin:
Sedimentary, Igneous, and Metaphorphic. */
class Rock{
private double weight;
private static int total=0;
private String origin;
public Rock(double w, String s){
setWeight(w);
System.out.println("Rock " + ++total + " is created");
origin = s;
}
public void roll(){
weight *=1.2;
}
public void setWeight(double d){ //成员方法
weight = d>0?d:1;
}
public double getWeight( ){
return weight;
}
public String getOrigin(){
return origin;
}
}
public class RockTest{
public static void main(String args[]){
String s[]={"Sedimentary","Igneous","Metamorphic"};
Rock rock[]= new Rock[3];
for (int i=0; i<3; i++)
rock[i]=new Rock(10,s[i]);
rock[1].roll();
System.out.println("The " + rock[1].getOrigin() + " rock 2 has " + rock[1].getWeight() + " kg in weight after rolling.");
}
}
class Rock{
double weight;
static int total=0;
public Rock(double w){
setWeight(w);
System.out.println("Rock " + ++total + " is created");
}
void roll(){
weight *=1.2;
}
void setWeight(double d){ //成员方法
weight = d>0?d:1;
}
double getWeight( ){
return weight;
}
}
public class RockTest{
public static void main(String args[]){
Rock rock[]= new Rock[3];
for (int i=0; i<3; i++)
rock[i]=new Rock(10);
rock[1].roll();
System.out.println("The rock 2 has " + rock[1].getWeight() + " kg in weight after rolling.");
}
}
// I have corrected your program as follows:
import javax.swing.*;
public class speedofsound {
public static void main(String[] args) {
String input;
int medium = 0;
medium = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter 1, 2 or 3 \nPick a medium : 1 for Air, 2 for Water and 3 for Steel"));
switch (medium) {
case 1:
System.out.println ( " You have chosen Air" ); break;
case 2:
System.out.println ( " You have chosen Water");break;
case 3:
System.out.println (" You have chosen Steel");break;
default:
System.out.println ("Invalid choice");
}
}
}
Is your JLabel's instance on a background painted by the Graphics in AWT?
If the background is made by the Graphics's object, there will be a trouble. The stuff in AWT is considered as heavyweighted while the stuff, such as JLabel's instance in swing, lightweighted. The heaveweighted stuff always "overwrites" the lightweighted one.
Solution:
Use Label in AWT rather than JLabe in swing.
import java.util.*;
class cat{
String name;
int age;
double weight;
String breed;
boolean declawed;
public cat(String s, int n, double d, String bs, boolean dec){
age=n;
name = s;
weight = d;
breed = bs;
declawed = dec;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
public class OwensDianaWeek6Prog
{
public static void main (String[] args)
{
cat MyCat[] = new cat[3];
Scanner input = new Scanner(System.in);
for (int i=0; i<3; i++)
{
System.out.println("Enter the name of cat" + (i+1) + ":");
String name = input.next();
System.out.println("Enter the age of cat" + (i+1) + ":");
int age = input.nextInt();
System.out.println("Enter the weight of cat" + (i+1) + ":");
double weight = input.nextDouble();
System.out.println("Enter the breed of cat" + (i+1) + ":");
String breed = input.next();
System.out.println("Is cat" + (i+1) + "declawed? True or False:");
boolean declawed = input.nextBoolean();
MyCat[i] = new cat(name, age, weight,breed,declawed);
}
System.out.println("The cats over 3 with claws are:");
for (int i=0; i<3; i++)
if ((MyCat[i].age >= 3) && (MyCat[i].declawed == false)){
System.out.println("Name: " + MyCat[i].getName());
System.out.println("Age: " + MyCat[i].getAge() + "Years Old");
} //end if
} // main
} // end class OwensDianaWeek6Prog