The Official Programming Thread

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
any java coder expert here ?? why every class is inherited from object ?? koi acha guide kr de mje .. :(

Because that class contains the common behaviour that all classes can have such as the clone(), equals(), finalize() method.... It's just the way java was designed to have this type of class hierarchy
https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html

Also because of this we can store any type of object inside a Object reference variable... This is another way to implement generalized code instead of using generics

Code:
Object mystring = new String("asd");
like this
 

baqai

Well-known member
Dec 9, 2008
1,711
0
41
47
Karachi
www.saquibbaqai.com
guys its been some time i have programmed, i am coming from C and ActionScripting background, i want to make a HTML5/CSS based page in which data is loaded as per user selection

1. want to select school branch
2. it should load the classes in the school
3. Select the class and it should load the students in the class
4. Select a student and his/her profile (name, age, blood group etc) along with picture should come

what way should i take to achieve this? do i need to go SQL with this or can it be achieved with xml? i did a similar thing in flash using xml and was wondering if that technique will work with html5?
 

staticPointer

PG LEGENDARY
Dec 7, 2012
3,266
0
41
افغانستان
www.pakgamers.com
Because that class contains the common behaviour that all classes can have such as the clone(), equals(), finalize() method.... It's just the way java was designed to have this type of class hierarchy
https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html

Also because of this we can store any type of object inside a Object reference variable... This is another way to implement generalized code instead of using generics

Code:
Object mystring = new String("asd");
like this


ok i understand .. i wanna develop the tax system in java like fbr ..do u know how to calculate tax ?? whats the formula ??
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
I'm working in OOP [java]. I am pretty good with the concepts till now. But I am confused. I don't knwo the applications of OOP. Like in ICP we work on Array we know, where to use them ,when to use them. For OOP, I don't know, where do i need to work on them. Please can someone calrify it to me in details? Mention or quote this post please.
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
I'm working in OOP [java]. I am pretty good with the concepts till now. But I am confused. I don't knwo the applications of OOP. Like in ICP we work on Array we know, where to use them ,when to use them. For OOP, I don't know, where do i need to work on them. Please can someone calrify it to me in details? Mention or quote this post please.

i wrote a few beginner tutorials for OOP a while back. maybe they can help you

http://www.pakgamers.com/forums/f27/[tutorial]-object-oriented-programming-basics-123121/
http://www.pakgamers.com/forums/f27/[tutorial]-object-oriented-programming-classes-131618/
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
Yaar I know these things. I don't see anything related to my answer. I am clear in concepts of Object Class, Static, Instance and so on. All 4 pillers of OOP infact too.I'm asking, how do I know when to use OOP. Where do I need these things. I am not familier with he applications of oop on where to use them. These classes and so.
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
Yaar I know these things. I don't see anything related to my answer. I am clear in concepts of Object Class, Static, Instance and so on. All 4 pillers of OOP infact too.I'm asking, how do I know when to use OOP. Where do I need these things. I am not familier with he applications of oop on where to use them. These classes and so.
Yaar basically you can do everything without OOP too but it's gonna be hard, messy, etc. OOP is there to improve the code design rather than introduce new programming basics. The basics are the same loops/control flow/etc. What OOP does is that it gives you a more flexible and well designed code that is easier to understand and helps modelling real life situations.

For example consider a company's payroll system. The payroll is different w.r.t to the designation of the employees. Without OOP you are gonna do everything in main() using functions and that's gonna be too messy ( Suppose there are 10-20 designations). With OOP you could create a base class that consists of all those properties that every employee does. Then make all the differet subclasses with their own method of calculating payroll. Basically we are doing the same thing but we are giving it a design that makes it easy to model real-life and easier to understand what is going on. We considered everything as objects.

Another example could be an airline booking system. Instead doing thing without objects, we consider OOP in which we can wrap things up in a single class that consists of properties like flight arrival/departure time, source, destination etc. Then make subclasses and instantiate them and feed data into those objects. That way each flight has its own data wrapped up inside their own object... makes thing easier to understand.

When you do OOP you can use UML (Unified Modelling Language) to make a graph of some sort which gives you a layout of how your program/code is constructed. When you get to higher levels, people don't check your code... they check the design. How the program is designed? That's what that matters. If you are doing things without OOP how will you explain them? You can't make a graph, its gonna be too messy.


EDIT:-
That aside OOP has other sorts of benefits too like data hiding. You don't want everybody to access each others data. Overall the main benefit is still the design and the way OOP manages the code.
 
Last edited:

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
Yaar basically you can do everything without OOP too but it's gonna be hard, messy, etc. OOP is there to improve the code design rather than introduce new programming basics. The basics are the same loops/control flow/etc. What OOP does is that it gives you a more flexible and well designed code that is easier to understand and helps modelling real life situations.

For example consider a company's payroll system. The payroll is different w.r.t to the designation of the employees. Without OOP you are gonna do everything in main() using functions and that's gonna be too messy ( Suppose there are 10-20 designations). With OOP you could create a base class that consists of all those properties that every employee does. Then make all the differet subclasses with their own method of calculating payroll. Basically we are doing the same thing but we are giving it a design that makes it easy to model real-life and easier to understand what is going on. We considered everything as objects.

Another example could be an airline booking system. Instead doing thing without objects, we consider OOP in which we can wrap things up in a single class that consists of properties like flight arrival/departure time, source, destination etc. Then make subclasses and instantiate them and feed data into those objects. That way each flight has its own data wrapped up inside their own object... makes thing easier to understand.

When you do OOP you can use UML (Unified Modelling Language) to make a graph of some sort which gives you a layout of how your program/code is constructed. When you get to higher levels, people don't check your code... they check the design. How the program is designed? That's what that matters. If you are doing things without OOP how will you explain them? You can't make a graph, its gonna be too messy.


EDIT:-
That aside OOP has other sorts of benefits too like data hiding. You don't want everybody to access each others data. Overall the main benefit is still the design and the way OOP manages the code.
Yeah. that's what I wanted. Thanks. :)
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
i dont know about all of that UML stuff but here is a very basic working example

Equipment
Spoiler: show
Code:
import java.util.Date;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Adnan
 */
public class Equipment
{
    String type;
    String serial;
    String model;
    Boolean warranty;
    String user;
    String location;
    
    public Equipment(String t, String s, String m, Boolean b)
    {
        type = t;
        serial = s;
        model = m;
        warranty = b;
    }
    public String getUser()
    {
        return user;
    }
    public String getLocation()
    {
        return location;
    }
    public String prettyPrint()
    {
        String output = "";
        output += "\nEquipment: " + serial;
        output += "\nAssigned to: " + user;
        return output;
    }
}
Location
Spoiler: show
Code:
import java.util.ArrayList;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Adnan
 */
public class Location
{
    String name;
    int no_of_offices;
    ArrayList<Employee> office_employees;
    Employee ITGuy;
    ArrayList<Equipment> office_equipment;
    
    public Location (String n, int o, Employee IT)
    {
        name = n;
        no_of_offices = o;
        ITGuy = IT;
        office_employees = new ArrayList();
        office_equipment = new ArrayList();
    }
    
    public void addEmployee (Employee em)
    {
        office_employees.add (em);
        em.setLocation(name); //better way to do
    }
    public void addEquipment (Equipment e)
    {
        office_equipment.add (e);
        e.location = name;
    }
    public String prettyPrint()
    {
        String output = "";
        output += "Location: " + name;
        output += "\nNumber of offices = " + no_of_offices;
        output += "\nAmount of equipment = " + office_equipment.size();
        return output;
    }
}
Employee
Spoiler: show
Code:
import java.util.ArrayList;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Adnan
 */
public class Employee
{
    String name;
    char gender;
    String title;
    String department;
    Employee supervisor;
    String location;
    String coordinates;
    Equipment current_equipment;
    ArrayList<Equipment> past_equipment;
    
    public Employee (String n, char g, String t, String d, String c)
    {
        name = n;
        gender = g;
        title = t;
        department = d;
        coordinates = c;
    }
    public void setCurrentEquipment (Equipment e)
    {
        //add a small routine to check if its assigned to another user. remove from him
        if (current_equipment != null)
            past_equipment.add (current_equipment);
        current_equipment = e;
        e.user = name; 
        //you can set the equipment location here too
    }
    public void setLocation (String l)
    {
        location = l;
    }
    public String prettyPrint()
    {
        String output = "";
        output += "Name: " + name;
        output += "\nCurrently assigned: " + current_equipment.serial;
        return output;
    }
}
Main
Spoiler: show
Code:
import static java.lang.Boolean.*;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Adnan
 */
public class InventorySystem
{
    public static void main (String [] args)
    {
        Equipment e1 = new Equipment("Computer", "A123", "X7", TRUE);
        Equipment e2 = new Equipment("Printer", "B123", "A2", FALSE);
        
        Employee em1 = new Employee("Newton", 'M', "Agent47", "Leage of Assassins", "1234");
        
        Location l1 = new Location("Gotham", 1, em1);
        
        em1.setCurrentEquipment(e1);
        l1.addEmployee(em1);
        l1.addEquipment(e1);
        System.out.println (e1.prettyPrint());
        System.out.println (e2.prettyPrint());
        System.out.println (em1.prettyPrint());
        System.out.println (l1.prettyPrint());   
    }
}
all of this is in java and may not cover all the scenarios and may have mistakes and can be improved upon. Just wanted to show you how it might work. Good for OOP starters. If you have any questions just ask
In employee class, in setLocation method. Why didn't we use Location reference variable? A little confusion.
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
Yaar I know these things. I don't see anything related to my answer. I am clear in concepts of Object Class, Static, Instance and so on. All 4 pillers of OOP infact too.I'm asking, how do I know when to use OOP. Where do I need these things. I am not familier with he applications of oop on where to use them. These classes and so.
well if you are well aware of the oop concepts then you should be able to come up with real life examples. look at you i partially modeled a 2D coordinate system in the example.

you may be able to do stuff without OOP but OOP gives you a much better method of achieving to goal. if you have any specific questions ask and i will try to clear up your confusions

- - - Updated - - -

In employee class, in setLocation method. Why didn't we use Location reference variable? A little confusion.
i did use the class variable. what is the alternate method you are proposing. write the code here and i will explain
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
well if you are well aware of the oop concepts then you should be able to come up with real life examples. look at you i partially modeled a 2D coordinate system in the example.

you may be able to do stuff without OOP but OOP gives you a much better method of achieving to goal. if you have any specific questions ask and i will try to clear up your confusions

- - - Updated - - -



i did use the class variable. what is the alternate method you are proposing. write the code here and i will explain
I am asking.
Look at employee class and check the setMethod. Look at the parameter you passes 'String'. Why not a 'location' reference variable?
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
I am asking.
Look at employee class and check the setMethod. Look at the parameter you passes 'String'. Why not a 'location' reference variable?
Oh i see. its just the way i defined the relations. you can see the location class has an employee object and the employee class has an equipment object and the

so the set in the employee is just a string where i a keeping a record of the employees location. i cannot make it have a location type ref variable too bcoz that would violate the relation upon which i built the solution
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
Oh i see. its just the way i defined the relations. you can see the location class has an employee object and the employee class has an equipment object and the

so the set in the employee is just a string where i a keeping a record of the employees location. i cannot make it have a location type ref variable too bcoz that would violate the relation upon which i built the solution
All the confusion right now in OOP is that is. I am not sure what you are saying still. I am confused on when to use reference variable as you did to interlink classes with each other. That's why I need proper mentor.
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
All the confusion right now in OOP is that is. I am not sure what you are saying still. I am confused on when to use reference variable as you did to interlink classes with each other. That's why I need proper mentor.
ok.

its all about the mental construct you make in your head. using that mental construct you will write your classes and interlink them to provide the whole solution. so of course there are multiple ways a problem can be solved.
In this case its very clear that there will be 3 major classes
  • Equipment
  • Employee
  • Location
the relationship between them will be such that : "an employee working at a certain location is working on a certain equipment there"

based on this statement you can define how your classes will interact with each other. lets see a few ways you can do that

1- a Location will have a list of its employees. each employee will be assigned an equipment. in this case a location object will have an array etc ref variable of employees. and an employee object will have an equipment reference variable. If i mix everything up like a location will have and employee ref var and an employee will have a location ref var i will be creating a paradox in my design.

2- an employee object has a location ref var and an equipment ref var and i can maintain a 4th class HR which has a hashmap or something which maintains employee location lists etc

if you have further confusions you can ask
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
ok.

its all about the mental construct you make in your head. using that mental construct you will write your classes and interlink them to provide the whole solution. so of course there are multiple ways a problem can be solved.
In this case its very clear that there will be 3 major classes
  • Equipment
  • Employee
  • Location
the relationship between them will be such that : "an employee working at a certain location is working on a certain equipment there"

based on this statement you can define how your classes will interact with each other. lets see a few ways you can do that

1- a Location will have a list of its employees. each employee will be assigned an equipment. in this case a location object will have an array etc ref variable of employees. and an employee object will have an equipment reference variable. If i mix everything up like a location will have and employee ref var and an employee will have a location ref var i will be creating a paradox in my design.

2- an employee object has a location ref var and an equipment ref var and i can maintain a 4th class HR which has a hashmap or something which maintains employee location lists etc

if you have further confusions you can ask
It cleared me a bit. But how do you know that you have to reference variable?
Spoiler: show

2- an employee object has a location REF VAR and an equipment REF VAR and i can maintain a 4th class HR which has a hashmap or something which maintains employee location lists etc
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
It cleared me a bit. But how do you know that you have to reference variable?
Spoiler: show

2- an employee object has a location REF VAR and an equipment REF VAR and i can maintain a 4th class HR which has a hashmap or something which maintains employee location lists etc
where-ever I am satisfying the relationships which i thought up in my mental construct (or i can call it the design too) i will use reference variables.

for other situations i can use any means possible. either a ref variable or a simple variable how ever it is convenient for me
 

asad3man

Omniscient
Jul 21, 2011
822
0
22
Karachi
where-ever I am satisfying the relationships which i thought up in my mental construct (or i can call it the design too) i will use reference variables.

for other situations i can use any means possible. either a ref variable or a simple variable how ever it is convenient for me
That's what I am asking. What's the criteria in your mind, when to use reference variable?
PM me your skype.
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
That's what I am asking. What's the criteria in your mind, when to use reference variable?
PM me your skype.
thats simple if it doesnt violate my design in any way i use a reference variable otherwise i use other means

sorry man i live in a part of the world were skype and other stuff like it are blocked and my vpn has expired
 
General chit-chat
Help Users
We have disabled traderscore and are working on a fix. There was a bug with the plugin | Click for Discord
  • No one is chatting at the moment.
  • L LegacyGamerGuy:
    About the Microsoft (and Sony) debate, I feel both have become too greedy and losing their minds over corporate greed. Personally, I will never buy any console next gen and switch to PC gaming where democracy reins.
    Link
  • L LegacyGamerGuy:
    No news about Google Play Store hacking. What is the source?
    Link
  • Link
  • Aciel Aciel:
    This is the error.
    Link
  • Aciel Aciel:
    Online session is enabled for UBL. I have an active subscription with my NayaPay card (it is already added to Google Payment Methods).
    Link
  • B Baghi:
    did you try setting up Google Payments? For UBL you may have to get it enabled for internet transactions, Naya should work without it!
    Link
  • Aciel Aciel:
    I tried both NayaPay, and UBL, but I keep getting "correct country selected..." error. I already have PK set there 🤦‍♂️
    Link
  • Aciel Aciel:
    Baghi said:
    Link
  • B Baghi:
    Yes
    Link
  • Aciel Aciel:
    Does your VISA Cards work on Google Play Store?
    Link
  • M murtaza12:
    XPremiuM said:
    Why? Was the site hacked or something?
    Yes
    Link
  • XPremiuM XPremiuM:
    GloriousChicken said:
    Everyone, please change your passwords.
    Why? Was the site hacked or something?
    Link
  • GloriousChicken GloriousChicken:
    Everyone, please change your passwords.
    Link
  • Necrokiller Necrokiller:
    The only valid thing from his pov he said in the video is AC dead since Black Flag. According to woke police that game was woke too. Welsh man in West Indies. So atleast he's consistent I guess lol
    Link
  • Link
  • XPremiuM XPremiuM:
    Necrokiller said:
    It's based on an actual real life person so I don't think the woke police have a valid case here.
    Nope. They have a very valid case. The above video explains it all.
    Link
  • Necrokiller Necrokiller:
    It's based on an actual real life person so I don't think the woke police have a valid case here.
    • Like
    Reactions: SolitarySoldier
    Link
  • XPremiuM XPremiuM:
    Meanwhile Ghost of Tsushima PC version is out now. Looks 100 times better than ASS Creed already.
    Link
  • XPremiuM XPremiuM:
    Did y'all see the new Assassin's Creed trailer? They finally made a AC set in Japan & then they put a negro as the male protagonist. Ubisoft is taking cues from Disney, and it isn't gonna end well for them, just like Disney. Go woke, go broke!
    Link
  • Necrokiller Necrokiller:
    First Fallout 4 update and now this 🤡
    Link
  • Necrokiller Necrokiller:
    MS and Bethesda continuing their streak of massive Ls 😬
    Link
  • Link
  • funky funky:
    Hello
    Link
  • NaNoW NaNoW:
    by closing down good studios
    Link
  • NaNoW NaNoW:
    well he is breaking barriers
    • Like
    Reactions: KetchupBiryani
    Link
    L LegacyGamerGuy: About the Microsoft (and Sony) debate, I feel both have become too greedy and losing their minds...