<< Test Bank Diversity and Society Race Ethnicity and Gender 5th Edition by Joseph F. Healey | Test Bank For Absolute C++ 5th Edition by Walter Savitch >> |
Chapter 2
Console Input and Output
(a) Anything with double quotes
(b) String variables
(c) Variables of type int
(d) All of the above
System.out.print(is fun!);
System.out.println( is fun!);
System.out.println( is fun!);
DecimalFormat percent = new DecimalFormat(0.00%);
System.out.println(percent.format(0.308));
DecimalFormat dfQuestion = new DecimalFormat(#0.##E0);
System.out.println(dfQuestion.format(12.7896987));
System.out.println(Wally Wonders);
System.out.print(Wally Wonders);
import java.text.NumberFormat;
NumberFormat nfMoney = NumberFormat.getCurrencyInstance();
System.out.println(nfMoney.format(100));
import java.text.DecimalFormat;
public class decimalClass
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat(00.00%);
System.out.println(df.format(.5732));
}
}
import java.util.Scanner;
public class ConsoleMultiply
{
public static void main(String[] args)
{
//Create the scanner object for console input
Scanner keyboard = new Scanner(System.in);
//Prompt the user for the first number
System.out.print(Enter the first integer: );
//Read the input
int firstNumber = keyboard.nextInt();
//Prompt the user for the second number
System.out.print(Enter the second integer: );
//Read the second number
int secondNumber = keyboard.nextInt();
System.out.println(firstNumber + * + secondNumber + is
+ firstNumber * secondNumber);
}
}
The format specifiers s and c are used for string and character representation, respectively.
/** void method that prints a phrase */
public void printPhrase()
{
System.out.println(Five purple people eaters were seen munching Martians!);
}
/** method that returns the value of PI */
public double piValue()
{
return 3.1415926535;
}
/** x and n are nonnegative integers */
public long power(int x, int n)
{
long result = 1;
/** check for positive numbers */
if((x >= 0) && (n >= 0))
{
/** raise x to the nth power */
for(int i = n; i > 0; i)
result *= x;
}
else
{
result = 0;
System.out.println(Fatal error.positive integers required!);
}
return result;
}
/** method to display a personalized greeting */
public static void greeting(String name)
{
System.out.println(Hello + name);
}
/** method to compare to integers for equality */
public static boolean isEqual(int x, int y)
{
return x == y;
}
The modifiers public and private before a method definition have a similar meaning. If the method is labeled public, there are no restrictions on its usage. If the method is labeled private, the method can only be used in the definition of another method of the same class.
Normal good programming practices require that all instance variables be private and typically most methods be public.
public class Appointment
{
private int startTime;
private int endTime;
private String dayOfWeek;
private String month;
private int day;
private int year;
/** Mutator methods */
public void setStartTime(int st)
{
/** valid range for military time is 0-2400 */
if((st >= 0) && (st <= 2400))
startTime = st;
}
public void setEndTime(int et)
{
/** valid range for military time is 0-2400 */
if((et >= 0) && (et <= 2400))
endTime = et;
}
public void setDayOfWeek(String dow)
{
/** Valid values are the strings Sunday Saturday */
if(checkDayOfWeek(dow))
dayOfWeek = dow;
else
System.out.println(Fatal error.invalid day of week value!);
}
public void setMonth(String m)
{
/** Valid values are strings January December */
if(checkMonth(m))
month = m;
else
System.out.println(Fatal errorinvalid month value!);
}
public void setDay(int d)
{
/** Valid days in a date are the integers 1 31 */
if((d >= 1) && (d <= 31))
day = d;
}
public void setYear(int y)
{
if(y >= 0)
year = y;
}
/** Accessor methods */
public int getStartTime()
{
return startTime;
}
public int getEndTime()
{
return endTime;
}
public String getDayOfWeek()
{
return dayOfWeek;
}
public String getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
/** Facilitator methods */
private boolean checkDayOfWeek(String d)
{
if(d.equalsIgnoreCase(Sunday) || d.equalsIgnoreCase(Monday) ||
d.equalsIgnoreCase(Tuesday) || d.equalsIgnoreCase(Wednesday) ||
d.equalsIgnoreCase(Thursday) || d.equalsIgnoreCase(Friday) ||
d.equalsIgnoreCase(Saturday) || d.equalsIgnoreCase(Sunday))
return true;
else
return false;
}
private boolean checkMonth(String month)
{
if(month.equalsIgnoreCase(January) || month.equalsIgnoreCase(February) ||
month.equalsIgnoreCase(March) || month.equalsIgnoreCase(April) ||
month.equalsIgnoreCase(May) || month.equalsIgnoreCase(June) ||
month.equalsIgnoreCase(July) || month.equalsIgnoreCase(August) ||
month.equalsIgnoreCase(September) ||
month.equalsIgnoreCase(October) ||
month.equalsIgnoreCase(November) || month.equalsIgnoreCase(December))
return true;
else
return false;
}
}
/** Precondition: All instance variables of the calling object have values.
Postcondition: A positive integer value equal to x to the power n is returned.
*/
/** Class constructors */
public Appointment()
{
startTime = 0;
endTime = 0;
dayOfWeek = ;
month = ;
day = 0;
year = 0;
}
public Appointment(int st, int et, String dow, String m, int d, int y)
{
setStartTime(st);
setEndTime(et);
setDayOfWeek(dow);
setMonth(m);
setDay(d);
setYear(y);
}
import java.util.Scanner;
import java.util.StringTokenizer;
public class Parser
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.print(Enter a sentence and Ill display each word you entered: );
String sentence = keyboard.nextLine();
//Parse the string into tokens and echo back to the user
StringTokenizer tk = new StringTokenizer(sentence, );
System.out.println(Here are the tokens: );
while(tk.hasMoreTokens())
{
System.out.println(tk.nextToken());
}
}
}
public class Student
{
private String name;
private String id;
private double gpa;
/** Constructors */
public Student()
{
name = null;
id = null;
gpa = 0.0;
}
public Student(String n, String i, double g)
{
name = n;
id = i;
gpa = g;
}
/** Accessor methods */
public String getName()
{
return name;
}
public String getID()
{
return id;
}
public double getGPA()
{
return gpa;
}
/** Mutator methods */
public void setName(String n)
{
name = n;
}
public void setID(String i)
{
id = i;
}
public void setGPA(double g)
{
if((g >= 0) && (g <= 4))
gpa = g;
}
/** Facilitator methods */
public String toString()
{
return (name + + id + + gpa);
}
public boolean equals(Student s)
{
return ((name.equalsIgnoreCase(s.name)) && (id.equalsIgnoreCase(s.id)) &&
(s.gpa == gpa));
}
}
public class StudentTest
{
public static void main(String args[])
{
Student firstStudent = new Student();
Student secondStudent = new Student(Wally Wonders, xyz557, 2.54);
System.out.println(First Student statistics: );
System.out.println(firstStudent.getName() +
+ firstStudent.getID() + + firstStudent.getGPA());
System.out.println(Second Student statistics: );
System.out.println(secondStudent.getName() +
+ secondStudent.getID() + + secondStudent.getGPA());
firstStudent.setName(Sharon Smith);
firstStudent.setID(wyh886);
firstStudent.setGPA(3.99);
System.out.println(firstStudent);
System.out.println(secondStudent);
System.out.println(Comparing student objects, evalutes to +
firstStudent.equals(secondStudent));
}
}
public void setDay(int d)
{
//Valid days in a date are the integers 1 31
if((d >= 1) && (d <= 31))
day = d;
}
public boolean setDay(int day)
{
if((day >=1) && (day <= 31))
{
this.day = day;
return true;
}
else
{
return false;
}
}
Chapter 12
UML and Patterns
Once the order is placed, the order will be delivered to your email less than 24 hours, mostly within 4 hours.
If you have questions, you can contact us here