0% found this document useful (0 votes)
52 views

Universidad Surcolombiana: Main Parameters, Overloaded Main

This document contains a Java code sample that demonstrates overloaded main methods. It includes: 1. A Java class with a main method that calls other overloaded main methods. 2. Multiple overloaded main methods with different parameters (int, String). 3. Comments explaining the main method signature and how to fix exceptions from empty arguments. 4. A section on constructors with examples and questions about constructor syntax.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Universidad Surcolombiana: Main Parameters, Overloaded Main

This document contains a Java code sample that demonstrates overloaded main methods. It includes: 1. A Java class with a main method that calls other overloaded main methods. 2. Multiple overloaded main methods with different parameters (int, String). 3. Comments explaining the main method signature and how to fix exceptions from empty arguments. 4. A section on constructors with examples and questions about constructor syntax.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIVERSIDAD SURCOLOMBIANA

TALLER NO. 1 JAVA PROGRAMMING


ING. MATILDE MONTEALEGRE MADERO, MSC. WEEK 3 – 2020

INGENIERIA DE SOFTWARE

NICOL YAJAIRA GONZALEZ MENESES


Cod. 20132123898

NEIVA
HUILA
2020-1

Main Parameters, Overloaded Main 3. To check if definition of method is correct


Create a proyect for WEEK3 and understand and execute 4. type of variables: local, instant, static.
the following code LINE by LINE. Fix it if necessary. package javabasicmainmethod;
Remember : 1. To find the psvm that is executable public class JavaBasiCMainMethod {
2. identify methods and classes. public static void main(String... cmd) {
//CALL OVERLOADED METHODS HERE /*Both main(String[]) and main(String...) can not be
main(private, cmd); declared in same file.java*/
main(20); // signature for main
main("Hello World Overloading main" + 3 + 5); //public static void main(String[] args){}
//CALL MORE OVERLOADED METHODS main() HERE //static public void main(String[] args) {}
} //public static void main(String[] args) {}
public static void main(String type, String[] args) { public static void main(String args) {
System.out.println("Hello world, \n Welcome to this System.out.println("List of Arguments " + args);}
class"); public static void main(int number) {
//if there are no arguments at run time it will throw an System.out.println("Hello exam parameter integer is = "
//Exception in thread "main" + number);
java.lang.ArrayIndexOutOfBoundsException: 0 main("Hello + World + Overloading + main " + 3 + " " +
// Argument size = 0 5); }
// to fix it }//End of class
//int size;
//size=args.length; 1. TO FIND THE PSVM THAT IS EXECUTABLE
//if (size != 0) { public class JavaBasiCMainMethod {
System.out.println("Size: "+ args.length); public static void main(String... cmd) {
System.out.println(args[0] + args[1]); //CALL OVERLOADED METHODS HERE
System.out.println(args[2]); //main(private, cmd);
System.out.println(args[3]); main(20);
//System.out.println("Arguments here: " + args[0] + " main("Hello World Overloading main" + 3 + 5);
" + args[1] + " " + args[2] + " " + args[3]); //CALL MORE OVERLOADED METHODS main() HERE
//} }
//print all arguments without throwing Java Exception in
thread "main" public static void main(String type, String[] args) {
java.lang.ArrayIndexOutOfBoundsException System.out.println("Hello world, \n Welcome to this class");
//since size iquals zero it doesn't run the for statement //if there are no arguments at run time it will throw an
//for (int i = 0; i < args.length; i++) { //Exception in thread "main"
// System.out.println("args [" + i + "] = " + args[i]); java.lang.ArrayIndexOutOfBoundsException: 0
//} // Argument size = 0
}//End of main // to fix it
//int size;
/*NOW overloaded main : overloaded methods are //size=args.length;
methods with the same name but different signature. //if (size != 0) {
Use the following methods main in the class. Verify if System.out.println("Size: "+ args.length);
these main methods can be used. */ System.out.println(args[0] + args[1]);
/*public static void main(String args) { System.out.println(args[2]);
System.out.println("Hello Exam 2"); System.out.println(args[3]);
}*/ //System.out.println("Arguments here: " + args[0] + "" +
args[1] + " " + args[2] + " " + args[3]);
//}
//print all arguments without throwing Java Exception in
thread "main" java.lang.ArrayIndexOutOfBoundsException
//since size iquals zero it doesn't run the for statement
//for (int i = 0; i < args.length; i++) {
// System.out.println("args [" + i + "] = " + args[i]);
//}
}// End of main

/*
* NOW overloaded main : overloaded methods are
methods with the same name but
* different signature. Use the following methods
main in the class. Verify if
* these main methods can be used.
*/ Read handout 1: Java Building blocks from page 18 to 41
/* and do the following.
* public static void main(String args)
{ System.out.println("Hello Exam 2"); } Exercise 1. Constructors
*/ package classdesign2019;
/* class Dog1 {
* Both main(String[]) and main(String...) can not be private String color;
declared in same file.java public Dog1(String color) {
*/ System.out.println("constructor");
// signature for main this.color = color;
//public static void main(String[] args){} // A .What happened if the line this.color = color is
//static public void main(String[] args) {} changed to color = color
//public static void main(String[] args) {} }
public static void main(String args) { public void printColor()
System.out.println("List of Arguments " + { System.out.println("color= " + color);}
args); /**
} * RTA: La asignación de la variable no tiene
efecto para ser invocada.
public static void main(int number) { */
System.out.println("Hello exam parameter // B. What happened if next line is uncommented
integer is = " + number);
// public dog() {}
main("Hello + World + Overloading + main "
/**
+ 3 + " " + 5);
* RTA: Error porque se necesita que el método
}
retorne algo o sino que sea void
}// End of class
*/
// public void Dog() {} // not constructor since it has
return type
}
class Cat {
private String color;
private int height;
private int length;
public Cat(int length, int theHeight) {
// length = this.length;
this.length = length;
height = theHeight;}
public void printInfo()
{
System.out.println("Cat length= " + this.length + "
height= " + height + " color= " + color);}
}
public class ClassConstructors {
public static void main(String[] args) {
//C. Given the classes above create and instance of class
Dog1 that assign a color to a Dog and prints its color.
//D. Create a white Cat with length 10 and height 12,
print the assignment
}
}

Exercise 2. Order of Initialization


//A. What is the order of initialization of following code
(explain)
package classdesign2019;
class Example {
private String name = "dog"; Question 2. What’s the output for the following code.
//private String name; Choose all that apply.
{System.out.println(name);} 4: public class Question2 {
private static int COUNT = 0; 5: String Name = "Jhon";
static {System.out.println(COUNT);} 6: public void setName(String name) {
{ 7: name = name;}
COUNT += 10; 8: public String getName()
System.out.println(COUNT);} { 9: return name;}
public Example() { 10: static public void main(String cmd[]) {
System.out.println("constructor");} 11: Question2 q2 = new Question2 ();
} 12: System.out.println(q2.getName);}
class Demo 13: }
{ static A. Error on line 5 B. Error on line 7 C. Error on line 9
{add(2);} D. Error on line 10 E. Error on line 12
static void add(int number)
{System.out.print(number + " ");} Question 3. Which of the following options, when
Demo() {//constructor inserted at //INSERT CODE HERE, will print out I’m
add(5);} Learning Java.
static {add(4);} public class Testing2 {
{add(6);}
// INSERT CODE HERE
//B. What if this code gets uncommented
{ System.out.println("I’m Learning Java"); }
// static {
// new Demo(); }
// } A. public void main (String[] args)
{add(8);} B. public void main(String args[])
} C. static public void main (String[] array)
public class OrderOfInitialization { public D. public static void main (String args)
static void main(String[] args) { E. static public void main (String main[])
new Example();
// C. uncomment the next code Question 4. The following numbered list of Java
// new Demo(); class components is not in any particular order.
}
Select the acceptable order of their occurrence in
}
any Java class (choose all that apply):
Question 1. What’s the output for the following code.
1 comments 2 import statement
public class Testing
{ public int LENGTH = 5; 3 package statement 4 methods
Testing () {LENGTH++;} 5 class declaration 6 variables
{System.out.print(" Block Two ");} A. 1, 3, 2, 5, 6, 4
{LENGTH = 10;System.out.print(LENGTH);} B. 3, 1, 2, 5, 4, 6
public static void swing() { C. 3, 2, 1, 4, 5, 6
System.out.print(" swing ");} D. 3, 2, 1, 5, 6, 4
public static void main(String... cmd) { Testing
t = new Testing(); Question 5. Which of the following examples defines
swing();} a correct Java class structure?
{System.out.print(" Block One ");}
A. #connect java compiler;
}
#connect java virtual machine;
A. Block One 5 Block One swing B. Block Two 10 Block
One swing C. Block Two 6 Block One swing class EJavaGuru {}
D. Block One 10 Block One swing E. compilation error B. package java compiler;
import java virtual machine;
class EJavaGuru {}
C. import javavirtualmachine.*;
package javacompiler;
class EJavaGuru recompilation.
{ void method1() D. Old Java code doesn’t need recompilation
{} when newer versions of JVMs are released.
int count;
}
D. package javacompiler; Question 8. A class Course is defined in a package
import com.ejavaguru. Given that the physical
javavirtualmachine.*; class location of the corresponding class file is
EJavaGuru { /mycode/com/ejavaguru/Course.class and
void method1() {} execution takes place within the mycode directory,
int count; which of the following lines of
} code, when inserted at // INSERT CODE HERE, will
E. #package javacompiler; import the Course class into the
$import javavirtualmachine; class MyCourse?
class EJavaGuru { // INSERT CODE HERE
void method1() {} class MyCourse {
int count; Course c;
} }
F. package javacompiler; A. import mycode.com.ejavaguru.Course;
import B. import com.ejavaguru.Course;
javavirtualmachine; Class C. import mycode.com.ejavaguru;
EJavaGuru { D. import com.ejavaguru;
void method1() {} E. import mycode.com.ejavaguru*;
int count; F. import com.ejavaguru*;
}
Question 9. Examine the following code:
Question 6. Given the following contents of the Java class Course {
source code file MyClass.java, select the String courseName;
correct options: }
// contents of MyClass.java class EJavaGuru {
package com.ejavaguru; public static void main(String args[])
import java.util.Date; { Course c = new Course();
class Student {} c.courseName = "Java";
class Course {} System.out.println(c.courseName);
A. The imported class, java.util.Date, can }
be accessed only in the class Student. }
B. The imported class, java.util.Date, can be Which of the following statements will be true if the
accessed by both the Student and Course classes. variable courseName is defined as a private
C. Both of the classes Student and Course variable?
are defined in the package com.ejavaguru. A. The class EJavaGuru will print Java.
D. Only the class Student is defined in the B. The class EJavaGuru will print null.
package com.ejavaguru. The class Course is C. The class EJavaGuru won’t compile.
defined in the default Java package. D. The class EJavaGuru will throw an exception at
runtime.
Question 7. What is the meaning of “write once, run
anywhere”? Select the correct options: Question 10. What is the output for question 9 as it
A. Java code can be written by one team member is.
and executed by other team members.
B. It is for marketing purposes only. A. Java B. null C. 0 D. -1 E. 1
C. It enables Java programs to be compiled once
and can be executed by any JVM without

You might also like