Q1. Modify, compile, and run the HelloWorld program so that it prints the following
"This is my first Java program" (instead of "Hello world")
Q2. Please play around with the classpath settings with -classpath or -cp option and CLASSPATH environment
Q1. Create a new NetBeans project called "MyOwnHelloWorld" from scratch.
The program should display the following text when it runs.
This is my own NetBeans project, and I am damn proud of it.
Q1. Modify
ConditionalOperator.java as adding the following lines of code at the appropriate place, compile and run the code
-
int salary = 100000;
-
Print "You are rich!" if the salary is over 50000. Print "You are poor!" otherwise.
Do the homework both using command line tools and using NetBeans.
Q1. Modify GreatestValue.java code so that it displays the greatest number and smallest number .
1. Modify
GetInputFromKeyboard.java as following, compile and run the code
- Make the program to ask the following question
- Please enter your mother's age:
- Display added value of your age and your mother's age as following:
- The added value of your age and your mother's age is <whatever number>!
- Implement the following logic
- If the added value is over 200, display
- Hello <name>! You and your mother must be old!
- Otherwise
- Hello <name>! You and your mother must be young!
Do the homework either at the command line or using NetBeans.
1.
Modify GetInputFromKeyboardJOptionPane.java as following, compile and run the
code
- Make the program to ask the following question
- Display the entered age as following
- If the age is over 100, display
Hello <name>, You are old!
- Otherwise
Hello <name>, You are young!
Do the
homework either at the command line or using NetBeans.
Homework
1. Modify
Grades.java as following, compile and run the
code.
- If the average is greater than 90 (average > 90), display "You worked
too hard!" (instead of :-))
- If the average is greater than 60 (average > 60) and less than or
equal to 90 (average <= 90), display "You did OK."
- If the average is less than or equal to 60 (average <= 60), display
"You need to do some work."
Homework:1.
Modify
NumWords.java as following,
compile and run the code
- Ask the user to enter another number using the following code fragment at
the appropriate place.
- inputAnother =
Integer.parseInt(JOptionPane.showInputDialog
("Enter another number"));
- Compute the average of the two numbers and display the
average.
Homework:1.
1.
Modify
ArraySample.java as following,
compile and run the code
- Just before the for loop that
prints out the value of each entry of the ages[] array (bold fonted code
fragment above), create another for
loop in which a value of 100 is assigned to the first entry of the
array, ages[0], 101 to the next entry of the array, ages[1], and so on.
Do the homework either at the command line or using
NetBeans.
2.Create a new NetBeans project which contains
NamesAndAgesOfMyFamily.java source file as
following:
- Create a two dimentional array that contains the names and hobbies of your
family members.
- Display the names and hobbies of your family members as following.
You have to use either for or while loop.
- Sang Shin: Golf, Tennis, Swimming
- Young Shin 90 years old: Walking, Reading
- Daniel Shin 20 years old: Snowboarding, Golf
Hint: You can see an example of
2-dimentional array from
"Array
of Arrays" Java tutorial
Assignment 10 (Due Date: 16/10) (This assignment is optional, But I would appreciate those who can do it.)
Homework:1.
Create a new NetBeans project which contains
GreatestNameLength.java source file as
following:
- Ask the full names of three of your family members using showInputDialog() method of the JOptionPane class (as it is done in the
GreatestNumber program).
- Display the name of the family member who has the longest first name as
following. (If there is a tie, just display one who is older.)
- Daniel Shin has the longest first name
Hint: You will
have use various methods of String class. The following is an example code
fragment you might be able to use. Please feel free to use alternative
ways of doing the homework.
// Get the first name from a name string
using split() instance (non-static) method String[] nameArrayForPerson1
= person1NameInstance.split(" "); String[] nameArrayForPerson2 =
person2NameInstance.split(" ");
// Get the lengths of strings using
length() instance (non-static) method int lengthOfFirstNameOfPerson1 =
nameArrayForPerson1[0].length(); int lengthOfFirstNameOfPerson2 =
nameArrayForPerson2[0].length();
// Compare the lengths of the
first names between person1 and person2 if (lengthOfFirstNameOfPerson1
> lengthOfFirstNameOfPerson2){
System.out.println(nameArrayForPerson1[0] +
" has longer first name than "
+
nameArrayForPerson2[0]); }
|
1.
Modify
ClassAndObject.java as following,
compile and run the code
- Create another object instance of String class using new keyword. Initialize it with the
following value. Display it using Sytem.out.println(..) method.
- "I am another object instance of String class"
1.
Write
StaticAndInstanceMethods2.java as following, and compile and see what
compile errors you get. Fix any compile errors.
public class
StaticAndInstanceMethods2{
public static void
main( String[] args ){
// Create an instance of a
String class by using a keyword
"new". // For example, in
order to create an object instance of a String
class, // you will do the
following String
strInstance1 = new String ("I am object instance of a String
class");
// The following code will
generate a compile error since you are trying to
// invoke a instance method through a class. Fix this
compile error. char x =
String.charAt(2);
}
} |
Code 9.2.b:
StaticAndInstanceMethods2.java
1.
Modify
TestPassByValue.java as following.
Compile and run the application.
- Pass the second primitive type parameter to the test(..) method - you can
choose any primitive type parameter (such as long or boolean).
- Set the value of the second parameter before calling the test(..)
method. And change the value of the passed parameter within the test(..)
method as you did with the first parameter in Code-9.4 above.
- Modify the System.out.println(..) methods in the Code-9.4 above to display
the values of both the first parameter and second parameter.
1.
Write
EqualsTestInteger.java as
following. Compile and run the application.
- Compare object instances of Integer
class using the same testing code as in EqualsTestString.java
above.
Q1.
Modify StudentRecord.java as following
- Add setMathGrade(double grade) method
- Add setEnglishGrade(double grade) method
- Add setScienceGrade(double grade) method
Modify
StudentRecordExample.java as
following
- Create another StudentRecord object, call it myOwnRecord
- Call setName() method of
the myOwnRecord object
passing "myOwn"
as the value to set
- Display the name of the myOwnRecord
object
- Set Math grade of myOwnRecord object
- Set English grade of myOwnRecord object
- Set Science grade of myOwnRecord object
- Display the average grade of myOwnRecord object
Compile and run StudentRecordExample application
Q1. Modify
StudentRecord.java as
following
- Add another overloaded print() method which takes the following
three
parameters
- name
- grade average
- student count
Modify StudentRecordExmaple2.java as following
- Invoke the newly added print() method
1. Write a class called
Food under
foodpackage.fruitpackage pacakge
- Food.java should have the following package statement at the top
- package foodpackage.fruitpackage
- Add a couple of methods of your own in the Food.java.
Write a class called
FoodMain under
foodpackage.fruitpackage package
- FoodMain class creates an Food object
- FoodMain class then calls a method of Food object
Compile and run the code
Hint: The goal of this homework exercise is to let you experience s
two-level
package structure instead of just one. For example, the "
foodpackage" is a parent package of
the "
fruitpackage"
package. And each should have its own directory and they should reflect
the parent and child relationship of the package structure. What this
means is that you would have to create
foodpackage
directory first and then
fruitpackage
directory underneath it. Then you will create Food.java
and FoodMain.java under
fruitpackage directory.
1. Write TuftsStudent.java as following
- TuftsStudent class extends Student class
- Write a constructor of the TuftsStudent class as following
- public TuftsStudent(){
System.out.println("Inside TuftsStudent:Constructor");
}
2. Modify the Main.java to create an instance of
TuftsStudent class as following
- TuftsStudent student2 =new
TuftsStudent();
Student student3 =new
TuftsStudent();
3. Compile and run
the code. You should see the following:
- Inside Person:Constructor
Inside Student:Constructor
Inside
Person:Constructor
Inside Student:Constructor
Inside
TuftsStudent:Constructor
Inside Person:Constructor
Inside
Student:Constructor
Inside TuftsStudent:Constructor
1. In your TuftsStudent class, override getHobby() and
setHobby() methods of the Student class as following
- public String
getHobby(){
System.out.println("Inside TuftsStudent:getHobby()
method");
return "My hobby is "
+ super.getHobby();
}
public void setHobby(String s){
System.out.println("Inside TuftsStudent:setHobby()
method");
super.setHobby(s);
}
2. Change Main.java to invoke setHobby() and
getHobby() methods of the newly created TuftsStudent object instances as
followoing.
- // set hobbies of student2 and
student3
student2.setHobby("swimming");
student3.setHobby("dancing");
// get hobbies of student2 and
student3
String hobby2 =
student2.getHobby();
System.out.println("Hobby of student2 " +
hobby2);
String hobby3 =
student3.getHobby();
System.out.println("Hobby of student3 " + hobby3);
3. Compile and run the code. You should see the
following result.
- Inside Person:Constructor
Inside Student:Constructor
Inside
Person:Constructor
Inside Student:Constructor
Inside
TuftsStudent:Constructor
Inside Person:Constructor
Inside
Student:Constructor
Inside TuftsStudent:Constructor
Inside
TuftsStudent:setHobby() method
Inside TuftsStudent:setHobby()
method
Inside TuftsStudent:getHobby() method
Hobby of student2 My hobby
is swimming
Inside TuftsStudent:getHobby() method
Hobby of student3 My
hobby is dancing
1. Write
another class called Teacher.java as following
- Teacher class extends Person class
- Teacher clsss also has the following method
- //
Override getName() method of the parent class
public String
getName(){
System.out.println("Teacher: getName()");
return "Maybe Passionate Teacher"
+ super.getName();
}
2. Modify
the Main.java in which, getName() method of the Teacher object gets called
3.
Compile and run the code. You should see the following result.
- C:\myjavaprograms>java -classpath . polypackage.Main
Inside
Person:Constructor 2 receiving two parameters: Sang, 1 Dreamland
Inside
Student:Constructor 2 receiving two parameters: Sang, 1 Dreamland
Inside
Person:Constructor 2 receiving two parameters: Young, 2 Dreamland
Inside
Employee:Constructor 2 receiving two parameters: Young, 2 Dreamland
Inside
Person:Constructor 2 receiving two parameters: Wende, 21 New York
Inside
Teacher:Constructor 2 receiving two parameters: Wende, 21 New York
Student:
getName()
Person: getName()
Passionate Student Sang
Employee:
getName()
Person: getName()
Not so Passionate Employee
Young
Teacher: getName()
Person: getName()
Maybe Passionate Teacher
Wende
1. Define another abstract method
in the LivingThing.java as following
- public abstract void dance(String dancingStyle);
2. Implement a concrete method in
the Human.java that implements the dance() abstract method.
- public void dance(String ds){
System.out.println("Human
dances..." + ds);
}
3. Compile and run the code
Define your own package (for example, myownpackage)
Define an interface called
PersonInterface.java
under the package above. This interface containt the following
abstract
methods.
- void setName(String myName);
- void setAge(int myAge);
Define another interface called
SportInterface.java
under the package above. This interface containt the following
abstract
methods.
- String getMyFavoriteSport();
- void setMyFavoriteSport(String sportName);
- int howMuchItCostToPlayThisSport();
Define the third interface called
HobbyInterface.java
under the package above. The interface contains the following
abstract methods.
- String whatIsMyHobby();
- void setMyHobby(String hobby);
Write a class called
SportAndHobbyImpl.java
that implements the all three interfaces defined above -
PersonInterface, SportInterface and
HobbyInterface interfaces.
Provide whatever approproate business logic in the methods that
implement the abstract methods defined in these three interfaces.
5. Write
MyMain.java as
following.
- It contains the main method.
- It creates two object instances of the SportAndHobbyImpl class as
following.
- SportAndHobbyImpl x1= new SportAndHobbyImpl();
- SportAndHobbyImpl x2= new SportAndHobbyImpl();
- It sets the Name field of the object instances above using the setName(String myName)
method of SportInterface interface
as following.
- x1.setName("Sang Shin");
- x1.setAge(20);
- x2.setName("James Gosling");
- x2.setAge(30);
- It sets other fields - MyFavoriteSport and MyHobby - via
setMyFavoriteSport(String sportName) and setMyHobby(String
hobby) methods.
- It computes theCostToPlayThisSport
as following:
- Check if x1 is a type of SportInterface or not using instanceof keyword as following
- boolean b1 = x1 instanceof SportInterface;
- Check if x2 is a type of HobbyInterface or not using instanceof
keyword as following
- boolean b2 = x2 instanceof HobbyInterface;
- Display the Name, Age, MyFavoriteSport, MyHobby,
theCostToPlayThisSport fields of the two object instances.
- Display the boolean results above
Q1. Using JDBC, Create a table
COFFEES
Table COFFEES has following fields
COF_NAME varchar(32)
SUP_ID int
PRICE float
SALES int
TOTAL int
Q2. After creating table, Insert Records in the table COFFEES.
Your program should work as follows
Ask the user before inserting a record.
If user says yes, then get the value of fields from him
After receiving values from user, insert the record
Again ask the user if he want to insert a record, i.e. go to step1.
Repeat these steps until user answers NO
Q3. After Q2 is finished, display all the record in table format.