Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java OOP / Advanced Programming

Options

Comments

  • Registered Users Posts: 11,690 ✭✭✭✭Skylinehead


    I'll do the first question now. No guarantees on the others, later maybe ;)

    a)
    1. False
    2. True
    3. False
    4. True I think, false and false = true?

    b)

    public boolean isPositive (int x)
    {
    if (x > 0)
    return true;
    else return false;
    }

    c)

    public int maxNumber (int x, int y)
    {
    if (x >= y)
    return x;
    else return y;
    }

    * assuming of course the maximum of two integers is the biggest one? Badly worded question.

    d)

    public int numDivisors (int x)
    {
    int count = 0;
    for (int divisor = 2; divisor < x; divisor++)
    {
    if (x % divisor == 0)
    count++;
    }
    return count;
    }


    Boards is wrecking my indentation, hope you get the jist.


  • Registered Users Posts: 19 Skin10


    Regarding 1a

    I had
    1:False ( 0 not = 0 is false as 0 == 0 )
    2True ( if both operators are true then the answer is true when using &&)
    3:True ( unless both operators are both false ) other wise always true..
    4:True( Not False= is True i think) & false & True ( Not Sure)
    5:True ( False == False)

    1b

    public boolean isPositive( int x)\\i left out the public ( so not sure if that was correct)\\
    if ( x>0)
    return true
    else
    Return false

    1c
    Not sure as the wording is funny.
    I thought they were looking for the max of both numbers so i had the following.

    public int Totalnum(int x,int y, int total)

    int total = x+y
    return total
    but if it was the max of both int values then i would have answered the same.


    1d


  • Registered Users Posts: 19 Skin10


    Any one out there any more help on the above exam paper..


  • Registered Users Posts: 11,690 ✭✭✭✭Skylinehead


    Q2 a)

    i
    s
    s
    r
    i
    s

    b)
    public static int shortestWord (String s)
    {
    String[] splitWords = s.split(" ");
    int shortest = splitWords[0].length();

    for(int i = 1; i < splitWords.length; i++)
    {
    if(splitWords.length() < shortest)
    {
    shortest = splitWords.length();
    }
    }
    return shortest;
    }

    http://pastebin.com/QRfp82fs for correct indentation.


  • Registered Users Posts: 4 tod48


    hello , did you get my private message


  • Advertisement
  • Registered Users Posts: 1 avgwhitedude


    any answer for question 4c ?? and explanation?


  • Closed Accounts Posts: 3,152 ✭✭✭ozt9vdujny3srf


    def you_can_use()
        'Code Tags to rpotect your indentation. 
    }
    


  • Registered Users Posts: 4 tod48


    Why are you so concerned about the answers to these questions ?The repeat exam will hardly be the same


  • Registered Users Posts: 19 Skin10


    Thw repeat exam will not be the same but after talking with Ray last week he told me to go over the last exam as it will not change much at all ..


  • Registered Users Posts: 4 tod48


    can i get your number please


  • Advertisement
  • Registered Users Posts: 19 Skin10


    Q3
    Any help on below

    public televisionChannel (String Name,int number,Boolean HDTV)
    {
    String Name "";
    Int number = 0;
    Boolean HDTV =false;
    }
    Getter/Assessor

    public String getName()
    {
    Return name;
    }
    public int getNumber()
    {
    return number;
    }
    public boolean getHDTV()
    {
    return false;
    }

    Setter/Mutators

    public void setName(String name)
    {
    this.name=name;
    }
    public void setNumber(int number)
    {
    this.number=Number;
    }
    public void setHDTV (boolean HDTV)
    {
    this.HDTV=HDTV;
    }


  • Registered Users Posts: 4 tod48


    the mutators dont need "this" and the hdtv is type......


  • Registered Users Posts: 4 ohnonotagain


    Hi,
    I'm also doing the repeat exam and have been unable to get in touch with the lecturer re what to expect. I am going over the past paper also and hoping that should cover me. !!


  • Registered Users Posts: 1,460 ✭✭✭Ishmael


    public class TeleChan {
    
    	
    	private String name;
    	private int number;
    	private boolean hdtv;
    	
    	//Question 3A Default Constructor
    	public TeleChan() {
    		super();
    		this.name = "";
    		this.number = 0;
    		this.hdtv = false;
    	}
    
    	//Question 3B - Constructor using parameters
    	public TeleChan(String name, int number, boolean hdtv) {
    		super();
    		this.name = name;
    		this.number = number;
    		this.hdtv = hdtv;
    	}
    
    	// Question 3 C & D - Getters and Setters, Accessors and Mutators
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getNumber() {
    		return number;
    	}
    
    	public void setNumber(int number) {
    		this.number = number;
    	}
    
    	public boolean isHdtv() {
    		return hdtv;
    	}
    
    	public void setHdtv(boolean hdtv) {
    		this.hdtv = hdtv;
    	}
    
    	//Question 3E - toString Method
    	@Override
    	public String toString() {
    		return "TeleChan [name=" + name + ", number=" + number + ", hdtv="
    				+ hdtv + "]";
    	}
    
    	//Question 3F - equals method
    	public boolean equals(TeleChan aTeleChan)
    	{
    		boolean result = false;
    		
    		if((aTeleChan.getName().equals(this.name)) && (aTeleChan.getNumber() == this.number) && (aTeleChan.isHdtv()==this.hdtv))
    		{
    			result = true;
    		}
    			
    		return result;
            }
    
    	//Question 3G - Return number of digits
    	public int numDigits()
    	{
    		String newString = ""+this.number;
    		return newString.length();
    	}
    
    	//Question 3H - Return String if HD or not
    	public String isDigHDTV()
    	{
    		if(this.hdtv)
    		{
    			return "Digital T.V.";
    		}
    		else
    		{
    			return "Analogue T.V.";
    		}
    	}
    		
    }
    


  • Registered Users Posts: 19 Skin10


    Yea i have not had any ore response from Ray since the first call.
    I am going over the past papers also.

    i am on on Q4 2012

    The first run trough with out brackets

    Q4a

    a : 17.8
    b: 6
    c:5.57143
    d:10
    e:10.5

    with Brackets
    ????


    Q4b
    When i first saw this in the exam i went on a mad tangent but after messing around with for loops its only 6 lines of code..

    draw the star thingy

    public class starprint
    {
    public static void main(String [] args)
    {
    for (int i=0;i<6;i++)
    {
    for(int j=i;j>0;j--)
    System.out.print("*");
    System.out.println();
    }
    }
    }

    Q4c
    What will be the values of (i) m, (ii) nums[m] and (iii) count after the code
    has executed?
    [

    After looking at the code and then after trying to run it i noticed loads of errors in the code .
    So i am guessing this was a trick question as it would not run or just a miss print , so not sure of out put..

    int count = 0;
    []int nums = { 21, 10, 15, -10, 10, 5, 8, 10, 3, 12);// should it not be
    int [] nums ? and the start has a curley bracket the finish has a normal bracket.

    int m = 0;
    for(int i = 1; i < nums.length; i = i + 1)
    if (nums < nums [m])
    {
    count = count + 1;
    m=i
    }


  • Registered Users Posts: 19 Skin10


    Can you trace through your answer still trying to get this one ..


  • Registered Users Posts: 19 Skin10


    Q2 a)

    i
    s
    s
    r
    i
    s

    b)



    http://pastebin.com/QRfp82fs for correct indentation.

    can you trace through your answer , still trying to get this one..


  • Registered Users Posts: 4 ohnonotagain


    Thanks Ishmael:D
    I am answering myself and will compare my answers to yours to make sure I'm on right track.

    However, regarding question one I think I got different answers to the guys earlier
    1. false 2.true. 3. true 4.false 5.true

    the last two questions there iv and v

    iv true and false and true = false (because they are not all equal)?
    v.both of these in brackets are false so both are equal which is true?
    thanks a million
    ;)


  • Registered Users Posts: 4 ohnonotagain


    the array length or 'capacity' is eight starting at zero.

    int len = 6 so draw a diagram and put an x over the box nr six

    in box zero one and two put mon and tues and wed
    in box len-1....go to 6th index (ie 7th place) and go back one and put thurs
    in this box go back another and put friday and back another and put in saturday.

    so your boxes (or your array) has mon tues wed sat fri thurs and the rest are empty.

    change mon to friday so index o is now fri also.

    no it says trace through the length and get the character at position -4 from the last letter of each word. hence if you count saturday y is -1 a is -2 d is -3 and r is -4.


  • Registered Users Posts: 4 ohnonotagain


    "
    i am on on Q4 2012

    The first run trough with out brackets

    Q4a

    a : 17.8
    b: 6
    c:5.57143
    d:10
    e:10.5

    with Brackets
    ????
    "

    this question should be easy but a bit confusing...

    a) 21-16 = 5 and 5/5 equal to 1

    b) 5/9/2 (after adding 7+2)?????

    c) 21/7 is equal to three

    d) 2*4*3 is equal to 12"

    e) 42/4 is equal to 10.5

    I haven't used brackets either. I know there is a rule ie do the addition and multiplication before disivion or subtraction. Here I just did what was easiest so anyone know the rules??? I think you can put the brackets wherever you like to get the answers you want so perphaps theoreticallly we have put the brackets where we want to work it out but not followed the rules for the first part of the question???? I'm taking a break now for a bit but will check back later.:cool:


  • Advertisement
  • Registered Users Posts: 1,460 ✭✭✭Ishmael


    Question 4 a)

    21 - 16 / 5 = 18 - truncation occurs due to division of two ints. I.E. 16 / 5 = 3 and not 3.2.
    (21 -16)/5 = 1

    5.0 / 2 + 7 / 2 = 2.5 + 3 = 5.5
    5.0 / (2 + 7) / 2 = 0.277777778

    3.0 + 18 / 7 = 3.0 + 2 = 5
    (3.0 + 18) / 7 = 3

    2 * 2 + 2 * 3 + "" = 10
    2 * (2 + 2) * 3 + "" = 24

    6 * 7 / 4 = 10
    6 * (7 / 4) = 6

    I think they are correct but i may be wrong. If you ever right real code without proper parenthesis, you should be shot.

    I tested these and it seems that the numbers are truncated but it depends on whether you are expected to account for this
    or not in your exam.

    =====================================


    Question 4 b)

    for(int i=0;i<=5;i++)
    {
    for(int j=0;j<i;j++)
    {
    System.out.print("#");
    }
    System.out.println("");
    }

    //prints a # symbol if j is < i, prints a return character when i = j. i varies from 1 to 5.

    =========================================

    Question 4 c)

    m = 3
    nums[m] = -10
    Count = 2


  • Registered Users Posts: 19 Skin10


    good luck to all 2moro ,


Advertisement